{"id":7018,"date":"2025-06-02T13:25:18","date_gmt":"2025-06-05T05:42:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7018"},"modified":"2025-06-05T13:25:18","modified_gmt":"2025-06-05T05:42:18","slug":"https-badgameshow-com-steven-185","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-185\/","title":{"rendered":"Swift \u8cc7\u6599\u5b58\u53d6\u5b8c\u5168\u6307\u5357\uff1a2025 \u6700\u65b0 Core Data \u5b58\u53d6\u8cc7\u6599\u6280\u5de7"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, Core Data, \u8cc7\u6599\u5b58\u53d6, Swift 2025, iOS \u958b\u767c\"><\/p>\n<h1>Swift \u8cc7\u6599\u5b58\u53d6\u5b8c\u5168\u6307\u5357\uff1a2025 \u6700\u65b0 Core Data \u5b58\u53d6\u8cc7\u6599\u6280\u5de7<\/h1>\n<p>\u5728 Swift \u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c<strong>Core Data<\/strong> \u662f\u4e00\u500b\u5f37\u5927\u7684\u8cc7\u6599\u5b58\u53d6\u6846\u67b6\uff0c\u5b83\u4e0d\u50c5\u80fd\u5e6b\u52a9\u4f60\u5132\u5b58\u548c\u7ba1\u7406\u8cc7\u6599\uff0c\u9084\u63d0\u4f9b\u4e86\u4e00\u500b\u76f4\u89c0\u7684 API \u4f86\u9032\u884c\u8cc7\u6599\u64cd\u4f5c\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Core Data \u4f86\u5b58\u53d6\u8cc7\u6599\uff0c\u4e26\u793a\u7bc4\u6700\u65b0\u7684\u6700\u4f73\u5be6\u8e10\u8207\u7bc4\u4f8b\u3002<\/p>\n<h2>Core Data \u67b6\u69cb\u6982\u8ff0<\/h2>\n<p>Core Data \u7684\u67b6\u69cb\u4e3b\u8981\u7531\u4e09\u500b\u6838\u5fc3\u7d44\u4ef6\u7d44\u6210\uff1a<\/p>\n<ol>\n<li><strong>Managed Object Model\uff1a<\/strong> \u5b9a\u7fa9\u8cc7\u6599\u6a21\u578b\u7684\u6a94\u6848\uff0c\u63cf\u8ff0\u8cc7\u6599\u7684\u7d50\u69cb\u53ca\u5176\u95dc\u4fc2\u3002<\/li>\n<li><strong>Managed Object Context\uff1a<\/strong> \u4e3b\u8981\u8ca0\u8cac\u8cc7\u6599\u7684\u5b58\u53d6\u548c\u7ba1\u7406\uff0c\u662f\u64cd\u4f5c\u8cc7\u6599\u7684\u6838\u5fc3\u7269\u4ef6\u3002<\/li>\n<li><strong>Persistent Store Coordinator\uff1a<\/strong> \u8ca0\u8cac\u5c07\u8cc7\u6599\u6301\u4e45\u5316\uff0c\u5c07\u8cc7\u6599\u5132\u5b58\u5230\u6a94\u6848\u6216\u8cc7\u6599\u5eab\u4e2d\u3002<\/li>\n<\/ol>\n<h2>\u5efa\u7acb Core Data \u6a21\u578b<\/h2>\n<p>\u9996\u5148\uff0c\u5728 Xcode \u4e2d\u5efa\u7acb\u4e00\u500b\u65b0\u7684 Core Data \u6a21\u578b\uff0c\u5229\u7528\u5176\u5716\u5f62\u5316\u4ecb\u9762\u4f86\u5b9a\u7fa9\u4f60\u7684\u8cc7\u6599\u6a21\u578b\u548c\u8cc7\u6599\u4e4b\u9593\u7684\u95dc\u4fc2\u3002\u78ba\u4fdd\u6a21\u578b\u8a2d\u8a08\u7b26\u5408\u4f60\u7684\u61c9\u7528\u9700\u6c42\uff0c\u4e26\u9069\u7576\u5730\u8a2d\u7f6e\u5be6\u9ad4\u53ca\u5176\u5c6c\u6027\u3002<\/p>\n<h2>\u5efa\u7acb Managed Object Context<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b <strong>Managed Object Context<\/strong>\uff0c\u9019\u662f\u7528\u4f86\u5b58\u53d6\u548c\u66f4\u65b0\u8cc7\u6599\u7684\u7269\u4ef6\u3002\u5728 Swift \u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u521d\u59cb\u5316\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let persistentContainer = NSPersistentContainer(name: \"YourModelName\")\npersistentContainer.loadPersistentStores { (storeDescription, error) in\n    if let error = error as NSError? {\n        fatalError(\"Unresolved error \\(error), \\(error.userInfo)\")\n    }\n}\nlet managedObjectContext = persistentContainer.viewContext\n<\/code><\/pre>\n<h2>\u5132\u5b58\u8cc7\u6599<\/h2>\n<p>\u8981\u5132\u5b58\u8cc7\u6599\uff0c\u6211\u5011\u4f7f\u7528 Managed Object Context\u3002\u4ee5\u4e0b\u662f\u5132\u5b58\u8cc7\u6599\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let entity = NSEntityDescription.entity(forEntityName: \"EntityName\", in: managedObjectContext)!\nlet managedObject = NSManagedObject(entity: entity, insertInto: managedObjectContext)\n\nmanagedObject.setValue(\"value\", forKey: \"key\")\n\ndo {\n    try managedObjectContext.save()\n    print(\"\u8cc7\u6599\u5132\u5b58\u6210\u529f\")\n} catch {\n    print(\"\u5132\u5b58\u8cc7\u6599\u6642\u767c\u751f\u932f\u8aa4: \\(error)\")\n}\n<\/code><\/pre>\n<h2>\u67e5\u8a62\u8cc7\u6599<\/h2>\n<p>\u4f7f\u7528 Managed Object Context \u67e5\u8a62\u8cc7\u6599\u7684\u7a0b\u5f0f\u78bc\u5982\u4e0b\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 value = result.value(forKey: \"key\") as! String\n        print(\"\u67e5\u8a62\u7d50\u679c: \\(value)\")\n    }\n} catch {\n    print(\"\u67e5\u8a62\u8cc7\u6599\u6642\u767c\u751f\u932f\u8aa4: \\(error)\")\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>\u5728\u4f7f\u7528 Core Data \u6642\uff0c\u4f60\u53ef\u80fd\u6703\u9047\u5230\u5404\u7a2e\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u8cc7\u6599\u6a21\u578b\u4e0d\u5339\u914d\uff1a<\/strong> \u78ba\u4fdd\u4f60\u7684\u8cc7\u6599\u6a21\u578b\u8207\u5df2\u5132\u5b58\u7684\u8cc7\u6599\u76f8\u7b26\uff0c\u7279\u5225\u662f\u5728\u4fee\u6539\u6a21\u578b\u5f8c\u3002<\/li>\n<li><strong>\u4e0a\u4e0b\u6587\u672a\u5132\u5b58\uff1a<\/strong> \u6bcf\u6b21\u4fee\u6539\u8cc7\u6599\u5f8c\uff0c\u90fd\u5fc5\u9808\u8abf\u7528 <code>save()<\/code> \u65b9\u6cd5\u4f86\u5132\u5b58\u4e0a\u4e0b\u6587\u3002<\/li>\n<li><strong>\u67e5\u8a62\u7d50\u679c\u70ba\u7a7a\uff1a<\/strong> \u6aa2\u67e5\u67e5\u8a62\u689d\u4ef6\uff0c\u78ba\u4fdd\u5b83\u5011\u6b63\u78ba\u4e14\u8cc7\u6599\u5b58\u5728\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>Core Data \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u57fa\u672c\u7684\u8cc7\u6599\u5b58\u53d6\uff0c\u9084\u53ef\u7d50\u5408\u5176\u4ed6\u6846\u67b6\u5982 <strong>SwiftUI<\/strong> \u548c <strong>Combine<\/strong> \u4f86\u958b\u767c\u66f4\u9ad8\u6548\u7684\u61c9\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>@FetchRequest<\/code> \u5728 SwiftUI \u4e2d\u8f15\u9b06\u5730\u7d81\u5b9a\u8cc7\u6599\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8cc7\u6599\u5b58\u53d6-\ud83d\udd25-Core-Data-\u5b58\u53d6\u8cc7\u6599_1675400685.306365.webp\" alt=\"Swift \u8cc7\u6599\u5b58\u53d6 \ud83d\udd25 Core Data \u5b58\u53d6\u8cc7\u6599\" title=\"Swift \u8cc7\u6599\u5b58\u53d6, Core Data, \u8cc7\u6599\u6a21\u578b, \u8cc7\u6599\u5eab, \u5132\u5b58\u8cc7\u6599, \u66f4\u65b0\u8cc7\u6599, \u522a\u9664\u8cc7\u6599\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q: Core Data \u662f\u5426\u9069\u5408\u6240\u6709\u985e\u578b\u7684\u61c9\u7528\u7a0b\u5f0f\uff1f<\/h3>\n<p>A: Core Data \u4e3b\u8981\u9069\u5408\u9700\u8981\u6301\u4e45\u5316\u8cc7\u6599\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u4f46\u5c0d\u65bc\u7c21\u55ae\u7684\u8cc7\u6599\u5b58\u53d6\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 UserDefaults \u6216\u5176\u4ed6\u8f15\u91cf\u7d1a\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h3>Q: \u5982\u4f55\u8655\u7406 Core Data \u7684\u7248\u672c\u63a7\u5236\uff1f<\/h3>\n<p>A: \u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>NSMigrationManager<\/strong> \u4f86\u8655\u7406\u8cc7\u6599\u6a21\u578b\u7684\u7248\u672c\u66f4\u65b0\uff0c\u4e26\u78ba\u4fdd\u8cc7\u6599\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<h3>Q: Core Data \u8207\u5176\u4ed6\u8cc7\u6599\u5b58\u53d6\u6846\u67b6\u76f8\u6bd4\u7684\u512a\u52e2\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A: Core Data \u63d0\u4f9b\u7684\u8cc7\u6599\u95dc\u806f\u6027\u548c\u67e5\u8a62\u6027\u80fd\u512a\u65bc\u8a31\u591a\u5176\u4ed6\u8cc7\u6599\u5b58\u53d6\u6846\u67b6\uff0c\u4e26\u4e14\u8207 Apple \u7684\u751f\u614b\u7cfb\u7d71\u7dca\u5bc6\u96c6\u6210\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684CoreData\u5b58\u53d6\u8cc7\u6599\uff0c\u4e26\u5b78\u7fd2\u5982\u4f55\u5efa\u7acb\u8cc7\u6599\u6a21\u578b\u3001\u5efa\u7acb\u8cc7\u6599\u5eab\u3001\u5132\u5b58\u8cc7\u6599\u3001\u66f4\u65b0\u8cc7\u6599\u3001\u522a\u9664\u8cc7\u6599\u7b49\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":7017,"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-7018","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-\u8cc7\u6599\u5b58\u53d6-\ud83d\udd25-Core-Data-\u5b58\u53d6\u8cc7\u6599_1675400685.306365.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Pc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7018","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=7018"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7018\/revisions"}],"predecessor-version":[{"id":13628,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7018\/revisions\/13628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7017"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}