{"id":6860,"date":"2025-05-30T14:42:50","date_gmt":"2025-06-05T07:15:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6860"},"modified":"2025-06-05T14:42:50","modified_gmt":"2025-06-05T07:15:50","slug":"https-badgameshow-com-steven-106","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-106\/","title":{"rendered":"\u30102025 \u6700\u65b0 Swift \u8cc7\u6599\u5eab\u64cd\u4f5c\u6559\u5b78 \ud83d\udcbe &#8211; \u4f7f\u7528 Core Data \u6846\u67b6\u7684\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u64cd\u4f5c\u8cc7\u6599\u5eab, Core Data, iOS, macOS\"><\/p>\n<h1>2025 \u6700\u65b0 Swift \u8cc7\u6599\u5eab\u64cd\u4f5c\u6559\u5b78 \ud83d\udcbe &#8211; \u4f7f\u7528 Core Data \u6846\u67b6\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u7531 Apple \u516c\u53f8\u958b\u767c\u7684\u958b\u6e90\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u7b49\u61c9\u7528\u7a0b\u5f0f\u3002Swift \u5177\u5099\u591a\u7a2e\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u80fd\u5920\u8f15\u9b06\u64cd\u4f5c\u8cc7\u6599\u5eab\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Core Data \u6846\u67b6\u9032\u884c\u8cc7\u6599\u5eab\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>Core Data \u6846\u67b6\u6982\u89bd<\/h2>\n<p>Core Data \u662f\u860b\u679c\u63d0\u4f9b\u7684\u4e00\u500b\u5f37\u5927\u8cc7\u6599\u5eab\u6846\u67b6\uff0c\u5141\u8a31\u958b\u767c\u8005\u4ee5\u7269\u4ef6\u7684\u5f62\u5f0f\u4f86\u7ba1\u7406\u8cc7\u6599\uff0c\u800c\u975e\u4ee5\u50b3\u7d71\u7684\u8868\u683c\u5f62\u5f0f\u3002\u9019\u4f7f\u5f97\u8cc7\u6599\u7684\u7406\u89e3\u8207\u64cd\u4f5c\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u548c\u4fbf\u5229\u3002<\/p>\n<h2>\u5efa\u7acb NSManagedObject \u7269\u4ef6<\/h2>\n<p>\u4f7f\u7528 Core Data \u9032\u884c\u8cc7\u6599\u5eab\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\u662f\u5efa\u7acb\u4e00\u500b <strong>NSManagedObject<\/strong> \u7269\u4ef6\u3002\u9019\u500b\u7269\u4ef6\u4ee3\u8868\u8cc7\u6599\u5eab\u4e2d\u7684\u4e00\u500b\u8cc7\u6599\u9805\uff0c\u4e26\u53ef\u4ee5\u5305\u542b\u591a\u7a2e\u5c6c\u6027\uff08\u5982\u5b57\u4e32\u3001\u6578\u5b57\u3001\u65e5\u671f\u7b49\uff09\u3002<\/p>\n<h2>\u65b0\u589e\u8cc7\u6599\u9805<\/h2>\n<p>\u4ee5\u4e0b\u662f\u7528\u65bc\u65b0\u589e\u4e00\u500b\u8cc7\u6599\u9805\u5230\u8cc7\u6599\u5eab\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext\n\nlet entity = NSEntityDescription.entity(forEntityName: \"EntityName\", in: managedObjectContext)\nlet newObject = NSManagedObject(entity: entity!, insertInto: managedObjectContext)\n\nnewObject.setValue(\"value1\", forKey: \"attribute1\")\nnewObject.setValue(\"value2\", forKey: \"attribute2\")\n\ndo {\n    try managedObjectContext.save()\n    print(\"\u8cc7\u6599\u65b0\u589e\u6210\u529f\uff01\")\n} catch {\n    print(\"\u5132\u5b58\u4e0a\u4e0b\u6587\u6642\u767c\u751f\u932f\u8aa4: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u6703\u5efa\u7acb\u4e00\u500b\u65b0\u7684 <strong>NSManagedObject<\/strong> \u7269\u4ef6\uff0c\u4e26\u5c07\u5176\u63d2\u5165\u5230\u8cc7\u6599\u5eab\u4e2d\uff0c\u63a5\u8457\u8a2d\u5b9a\u5c6c\u6027\u503c\uff0c\u6700\u5f8c\u5132\u5b58\u8cc7\u6599\u5230\u8cc7\u6599\u5eab\u3002<\/p>\n<h2>\u67e5\u8a62\u8cc7\u6599\u9805<\/h2>\n<p>Core Data \u4e5f\u63d0\u4f9b\u4e86 <strong>NSFetchRequest<\/strong> \u7269\u4ef6\u4f86\u67e5\u8a62\u8cc7\u6599\u3002\u4ee5\u4e0b\u662f\u5f9e\u8cc7\u6599\u5eab\u4e2d\u53d6\u5f97\u8cc7\u6599\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: \"EntityName\")\n\ndo {\n    let results = try managedObjectContext.fetch(fetchRequest)\n    for result in results as! [NSManagedObject] {\n        let attribute1 = result.value(forKey: \"attribute1\") as! String\n        let attribute2 = result.value(forKey: \"attribute2\") as! String\n        print(\"attribute1: \\(attribute1), attribute2: \\(attribute2)\")\n    }\n} catch {\n    print(\"\u67e5\u8a62\u8cc7\u6599\u6642\u767c\u751f\u932f\u8aa4: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u5f9e\u8cc7\u6599\u5eab\u4e2d\u53d6\u5f97\u6240\u6709\u8cc7\u6599\uff0c\u4e26\u5370\u51fa\u5176\u5c6c\u6027\u503c\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 Core Data \u64cd\u4f5c\u8cc7\u6599\u5eab\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u4e0a\u4e0b\u6587\u672a\u6b63\u78ba\u5132\u5b58\u6216\u67e5\u8a62\u6642\u6307\u5b9a\u7684\u5be6\u9ad4\u540d\u7a31\u932f\u8aa4\u3002\u5efa\u8b70\u5728\u6bcf\u6b21\u64cd\u4f5c\u5f8c\u90fd\u8981\u6aa2\u67e5\u932f\u8aa4\uff0c\u4e26\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u8cc7\u6599\u578b\u5225\u3002\u6b64\u5916\uff0c\u5c0d\u65bc\u8f03\u5927\u7684\u8cc7\u6599\u96c6\uff0c\u61c9\u8003\u616e\u4f7f\u7528\u5206\u9801\u6280\u8853\u4f86\u63d0\u5347\u6548\u80fd\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>Core Data \u4e0d\u50c5\u9069\u7528\u65bc\u7c21\u55ae\u7684 CRUD \u64cd\u4f5c\uff0c\u9084\u53ef\u4ee5\u8207\u5176\u4ed6\u6846\u67b6\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5c07\u8cc7\u6599\u8207\u7db2\u7d61\u8acb\u6c42\u7d50\u5408\uff0c\u5be6\u73fe\u96e2\u7dda\u8cc7\u6599\u5b58\u5132\uff0c\u6216\u662f\u5be6\u73fe\u8907\u96dc\u7684\u8cc7\u6599\u95dc\u806f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u64cd\u4f5c\u8cc7\u6599\u5eab-\ud83d\udcbe_1675397136.586827.webp\" alt=\"Swift \u64cd\u4f5c\u8cc7\u6599\u5eab \ud83d\udcbe\" title=\"Swift \u8cc7\u6599\u5eab\u64cd\u4f5c\u6559\u5b78\u793a\u4f8b\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>Q1: Core Data \u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A1: Core Data \u662f\u860b\u679c\u63d0\u4f9b\u7684\u4e00\u500b\u8cc7\u6599\u7ba1\u7406\u6846\u67b6\uff0c\u5141\u8a31\u958b\u767c\u8005\u4ee5\u7269\u4ef6\u7684\u65b9\u5f0f\u7ba1\u7406\u8cc7\u6599\uff0c\u4e26\u63d0\u4f9b\u8cc7\u6599\u6301\u4e45\u5316\u7684\u529f\u80fd\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8655\u7406 Core Data \u7684\u932f\u8aa4\uff1f<\/h3>\n<p>A2: \u5efa\u8b70\u5728\u6bcf\u6b21\u64cd\u4f5c\u5f8c\u90fd\u6aa2\u67e5\u932f\u8aa4\uff0c\u4e26\u4f7f\u7528 <code>do-catch<\/code> \u4f86\u6355\u6349\u548c\u8655\u7406\u53ef\u80fd\u7684\u4f8b\u5916\u60c5\u6cc1\u3002<\/p>\n<h3>Q3: Core Data \u53ef\u4ee5\u8207\u54ea\u4e9b\u5176\u4ed6\u6846\u67b6\u4e00\u8d77\u4f7f\u7528\uff1f<\/h3>\n<p>A3: Core Data \u53ef\u4ee5\u8207\u7db2\u7d61\u8acb\u6c42\u3001SwiftUI\u3001Combine \u7b49\u6846\u67b6\u4e00\u8d77\u4f7f\u7528\uff0c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u529f\u80fd\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u64cd\u4f5c\u8cc7\u6599\u5eab\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u5efa\u7acb\u8cc7\u6599\u5eab\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u8cc7\u6599\u5eab\u3002\u672c\u6587\u5c07\u4ecb\u7d39Swift\u8a9e\u8a00\u4e2d\u7684\u8cc7\u6599\u5eab\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u5efa\u7acb\u8cc7\u6599\u5eab\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":6856,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u64cd\u4f5c\u8cc7\u6599\u5eab-\ud83d\udcbe_1675397136.586827.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1ME","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6860"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6860\/revisions"}],"predecessor-version":[{"id":13616,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6860\/revisions\/13616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6856"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}