{"id":6656,"date":"2025-06-01T12:19:22","date_gmt":"2025-06-05T04:35:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-6\/"},"modified":"2025-06-05T12:19:22","modified_gmt":"2025-06-05T04:35:22","slug":"https-badgameshow-com-steven-6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-6\/","title":{"rendered":"Swift Core Data \u5b8c\u6574\u5be6\u4f5c\u6559\u5b78 &#8211; 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udcbe\ud83d\udd25"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, Core Data, Swift Core Data, iOS, macOS, \u8cc7\u6599\u5132\u5b58\"><\/p>\n<h2>Swift Core Data \u7c21\u4ecb<\/h2>\n<p>Swift Core Data \u662f\u4e00\u500b\u5f37\u5927\u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u5c07\u8cc7\u6599\u5132\u5b58\u5728 iOS \u548c macOS \u4e0a\u3002\u5b83\u80fd\u5920\u5e6b\u52a9\u4f60\u5efa\u7acb\u8907\u96dc\u7684\u8cc7\u6599\u6a21\u578b\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u500b\u6613\u65bc\u4f7f\u7528\u7684 API \u4f86\u5b58\u53d6\u8cc7\u6599\u3002\u672c\u6587\u5c07\u5e36\u4f60\u8d70\u904e\u5f9e\u5efa\u7acb\u8cc7\u6599\u6a21\u578b\u5230\u64cd\u4f5c\u8cc7\u6599\u7684\u5b8c\u6574\u6d41\u7a0b\uff0c\u4e26\u4ecb\u7d39 2025 \u6700\u65b0\u7684\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5efa\u7acb Data Model<\/h2>\n<p>\u5728\u958b\u59cb\u4f7f\u7528 Swift Core Data \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5efa\u7acb\u4e00\u500b <strong>Data Model<\/strong>\uff0c\u9019\u662f\u4e00\u500b\u63cf\u8ff0\u4f60\u7684\u8cc7\u6599\u7d50\u69cb\u7684\u6a94\u6848\uff0c\u5b83\u80fd\u5305\u542b\u591a\u500b <strong>Entity<\/strong>\uff0c\u6bcf\u500b Entity \u53ef\u4ee5\u6709\u591a\u500b <strong>Attribute<\/strong>\u3002\u9019\u4e9b Attribute \u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u5c6c\u6027\uff0c\u4f8b\u5982\uff1a<strong>String<\/strong>\u3001<strong>Integer<\/strong>\u3001<strong>Boolean<\/strong> \u7b49\u7b49\u3002<\/p>\n<p>\u8981\u5efa\u7acb Data Model\uff0c\u4f60\u53ef\u4ee5\u5728 Xcode \u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u7684\u6a94\u6848\uff0c\u9078\u64c7 <strong>Core Data<\/strong>\uff0c\u7136\u5f8c\u9078\u64c7 <strong>Data Model<\/strong>\uff0c\u63a5\u8457\u7d66\u5b83\u4e00\u500b\u540d\u7a31\uff0c\u4f8b\u5982 <strong>MyDataModel<\/strong>\uff0c\u7136\u5f8c\u6309\u4e0b <strong>Create<\/strong> \u6309\u9215\u3002<\/p>\n<h2>\u5efa\u7acb Entity<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728 Data Model \u4e2d\u5efa\u7acb\u4e00\u500b Entity\uff0c\u9ede\u64ca\u53f3\u4e0a\u89d2\u7684 <strong>+<\/strong> \u6309\u9215\uff0c\u9078\u64c7 <strong>New Entity<\/strong>\uff0c\u4e26\u7d66\u5b83\u4e00\u500b\u540d\u7a31\uff0c\u4f8b\u5982 <strong>User<\/strong>\uff0c\u7136\u5f8c\u6309\u4e0b <strong>Create<\/strong> \u6309\u9215\u3002<\/p>\n<h2>\u5efa\u7acb Attribute<\/h2>\n<p>\u5728 Entity \u4e2d\u5efa\u7acb Attribute\uff0c\u9ede\u64ca\u53f3\u4e0a\u89d2\u7684 <strong>+<\/strong> \u6309\u9215\uff0c\u9078\u64c7 <strong>New Attribute<\/strong>\uff0c\u7d66\u5b83\u4e00\u500b\u540d\u7a31\uff0c\u4f8b\u5982 <strong>name<\/strong>\uff0c\u9078\u64c7\u5176\u5c6c\u6027\u985e\u578b\uff0c\u4f8b\u5982 <strong>String<\/strong>\uff0c\u7136\u5f8c\u6309\u4e0b <strong>Create<\/strong> \u6309\u9215\u3002<\/p>\n<h2>\u5efa\u7acb Managed Object Model<\/h2>\n<p>\u63a5\u4e0b\u4f86\u5efa\u7acb\u4e00\u500b <strong>Managed Object Model<\/strong>\uff0c\u5b83\u5c0d\u61c9\u4f60\u7684 Data Model\u3002\u9019\u6bb5\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u653e\u5728\u4f60\u7684 <strong>AppDelegate<\/strong> \u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let managedObjectModel: NSManagedObjectModel = {\n    let modelURL = Bundle.main.url(forResource: \"MyDataModel\", withExtension: \"momd\")!\n    return NSManagedObjectModel(contentsOf: modelURL)!\n}()\n<\/code><\/pre>\n<h2>\u5efa\u7acb Persistent Store Coordinator<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5efa\u7acb\u4e00\u500b <strong>Persistent Store Coordinator<\/strong>\uff0c\u5b83\u5c0d\u61c9\u4f60\u7684 Managed Object Model\uff0c\u4ee5\u4e0b\u662f\u5efa\u7acb\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let persistentStoreCoordinator: NSPersistentStoreCoordinator = {\n    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)\n    let url = applicationDocumentsDirectory.appendingPathComponent(\"MyDataModel.sqlite\")\n    let failureReason = \"There was an error creating or loading the application's saved data.\"\n    do {\n        try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)\n    } catch {\n        var dict = [String: AnyObject]()\n        dict[NSLocalizedDescriptionKey] = \"Failed to initialize the application's saved data\" as AnyObject?\n        dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?\n        dict[NSUnderlyingErrorKey] = error as NSError\n        let wrappedError = NSError(domain: \"YOUR_ERROR_DOMAIN\", code: 9999, userInfo: dict)\n        NSLog(\"Unresolved error \\(wrappedError), \\(wrappedError.userInfo)\")\n        abort()\n    }\n    return coordinator\n}()\n<\/code><\/pre>\n<h2>\u5efa\u7acb Managed Object Context<\/h2>\n<p>\u6700\u5f8c\uff0c\u5efa\u7acb\u4e00\u500b <strong>Managed Object Context<\/strong>\uff0c\u5b83\u5c0d\u61c9\u4f60\u7684 Persistent Store Coordinator\uff0c\u4ee5\u4e0b\u662f\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let managedObjectContext: NSManagedObjectContext = {\n    let coordinator = persistentStoreCoordinator\n    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)\n    managedObjectContext.persistentStoreCoordinator = coordinator\n    return managedObjectContext\n}()\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 Core Data \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u7121\u6cd5\u627e\u5230\u8cc7\u6599\u6a21\u578b\u3001Persistent Store \u521d\u59cb\u5316\u5931\u6557\u7b49\u3002\u78ba\u4fdd\u4f60\u7684\u8cc7\u6599\u6a21\u578b\u6a94\u6848\u540d\u7a31\u6b63\u78ba\uff0c\u4e26\u6aa2\u67e5\u6240\u6709\u7684\u5c6c\u6027\u548c\u985e\u578b\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u5efa\u7acb\u4e86 Core Data \u7684\u57fa\u672c\u67b6\u69cb\u5f8c\uff0c\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u5b78\u7fd2\u5982\u4f55\u9032\u884c\u8cc7\u6599\u7684\u65b0\u589e\u3001\u67e5\u8a62\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002\u9019\u53ef\u4ee5\u900f\u904e\u4f7f\u7528 NSFetchRequest\u3001NSPredicate \u7b49\u4f86\u5be6\u73fe\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-Core-Data-\u5be6\u73fe-\ud83d\udcbe\ud83d\udd25_1675393245.509825.webp\" alt=\"Swift Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udd25\" title=\"Swift Core Data, \u8cc7\u6599\u5132\u5b58, \u61c9\u7528\u7a0b\u5f0f, \u7ba1\u7406\u8cc7\u6599, \u7bc4\u4f8b\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A (\u5e38\u898b\u554f\u984c\u89e3\u7b54)<\/h2>\n<h3>1. Core Data \u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>Core Data \u662f Apple \u63d0\u4f9b\u7684\u4e00\u500b\u6846\u67b6\uff0c\u7528\u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u8cc7\u6599\u6a21\u578b\uff0c\u7279\u5225\u662f\u7576\u8cc7\u6599\u9700\u8981\u6301\u4e45\u5316\u5132\u5b58\u6642\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 Core Data \u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728\u6dfb\u52a0 Persistent Store \u6642\u4f7f\u7528 do-catch \u4f86\u6355\u7372\u932f\u8aa4\uff0c\u4e26\u6839\u64da\u932f\u8aa4\u985e\u578b\u9032\u884c\u9069\u7576\u7684\u8655\u7406\u3002\u52d9\u5fc5\u6aa2\u67e5\u8cc7\u6599\u6a21\u578b\u548c Persistent Store \u7684\u914d\u7f6e\u3002<\/p>\n<h3>3. Core Data \u8207\u5176\u4ed6\u8cc7\u6599\u5132\u5b58\u65b9\u6848\u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>Core Data \u4e0d\u50c5\u50c5\u662f\u8cc7\u6599\u5132\u5b58\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u5b83\u9084\u63d0\u4f9b\u4e86\u8cc7\u6599\u6a21\u578b\u7684\u7ba1\u7406\u3001\u95dc\u806f\u548c\u8cc7\u6599\u9a57\u8b49\u7b49\u529f\u80fd\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8907\u96dc\u8cc7\u6599\u7d50\u69cb\u7684\u61c9\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528SwiftCoreData\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\uff0c\u53ef\u4ee5\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u5b8c\u6574\uff0c\u4e26\u4e14\u66f4\u5bb9\u6613\u7ba1\u7406\u8cc7\u6599\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftCoreData\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8b93\u4f60\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n","protected":false},"author":1,"featured_media":6655,"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-6656","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-Core-Data-\u5be6\u73fe-\ud83d\udcbe\ud83d\udd25_1675393245.509825.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Jm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6656","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=6656"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6656\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6655"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}