{"id":3110,"date":"2021-06-08T13:01:50","date_gmt":"2021-06-08T05:01:50","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3110"},"modified":"2021-06-08T13:01:50","modified_gmt":"2021-06-08T05:01:50","slug":"swift-%e5%9c%a8%e5%b7%b2%e6%9c%89%e9%a1%b9%e7%9b%ae%e4%b8%8a%e6%b7%bb%e5%8a%a0coredata%ef%bc%88%e4%b9%8b%e5%89%8d%e5%88%9b%e5%bb%ba%e6%97%b6%e6%9c%aa%e5%8b%be%e9%80%89use-core-data%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3110","title":{"rendered":"Swift &#8211; \u5728\u5df2\u6709\u9879\u76ee\u4e0a\u6dfb\u52a0CoreData\uff08\u4e4b\u524d\u521b\u5efa\u65f6\u672a\u52fe\u9009Use Core Data\uff09"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">1\uff0c\u95ee\u9898\u63cf\u8ff0<\/h5>\n\n\n\n<p>\uff081\uff09\u4f7f\u7528&nbsp;<strong>Core Data<\/strong>&nbsp;\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u3002\u5982\u679c\u9879\u76ee\u9700\u8981\u4f7f\u7528&nbsp;<strong>Cord Data<\/strong>\uff0c\u53ea\u9700\u8981\u5728\u521b\u5efa\u9879\u76ee\u7684\u65f6\u5019\uff0c\u52fe\u9009\u201c<strong>Use Core Data<\/strong>\u201d\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-15-1024x612.png\" alt=\"\" class=\"wp-image-3111\" width=\"659\" height=\"394\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-1024x612.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-300x179.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-768x459.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-830x496.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-350x209.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15-480x287.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-15.png 1044w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\u8fd9\u6837\u9879\u76ee\u521b\u5efa\u5b8c\u6bd5\u540e\uff0c\u9879\u76ee\u4e2d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a\u540c\u540d\u7684\u00a0<strong>xcdatamodeld\u00a0<\/strong>\u6587\u4ef6\u3002\u540c\u65f6\u00a0<strong>AppDelegate\u00a0<\/strong>\u4e2d\u4e5f\u4f1a\u751f\u6210\u76f8\u5173\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\uff083\uff09\u5982\u679c\u6211\u4eec\u9879\u76ee\u4e4b\u524d\u521b\u5efa\u65f6\u5fd8\u8bb0\u52fe\u9009\u4e0a\u201c<strong>Use Core Data<\/strong>\u201d\uff0c\u73b0\u5728\u53c8\u60f3\u7528\u00a0<strong>Cord Data\u00a0<\/strong>\u6846\u67b6\u7684\u8bdd\uff0c\u4e5f\u4e0d\u7528\u91cd\u65b0\u521b\u5efa\u5de5\u7a0b\uff0c\u53ea\u8981\u624b\u52a8\u914d\u7f6e\u4e0b\u5c31\u597d\u4e86\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2\uff0c\u624b\u52a8\u6dfb\u52a0 Cord Data \u652f\u6301<\/h5>\n\n\n\n<p>\uff081\uff09\u9996\u5148\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u00a0<strong>xcdatamodeld\u00a0<\/strong>\u6587\u4ef6\uff08<strong>Data Model<\/strong>\uff09\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-16-1024x740.png\" alt=\"\" class=\"wp-image-3112\" width=\"628\" height=\"454\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-1024x740.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-300x217.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-768x555.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-830x600.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-230x166.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-350x253.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16-480x347.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-16.png 1394w\" sizes=\"(max-width: 628px) 100vw, 628px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\u6587\u4ef6\u540d\u5efa\u8bae\u4e0e\u9879\u76ee\u540d\u4e00\u81f4\uff0c\u6bd4\u5982\u6211\u8fd9\u91cc\u53eb\uff1a<strong>hangge_1841.xcdatamodeld<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/06\/image-17.png\" alt=\"\" class=\"wp-image-3113\" width=\"328\" height=\"288\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-17.png 528w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-17-300x264.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-17-230x202.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-17-350x308.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-17-480x422.png 480w\" sizes=\"(max-width: 328px) 100vw, 328px\" \/><\/figure><\/div>\n\n\n\n<p>\uff083\uff09\u63a5\u7740\u6253\u5f00\u00a0<strong>AppDelegate.swift<\/strong>\uff0c\u5728\u91cc\u9762\u6dfb\u52a0\u00a0<strong>Core Data\u00a0<\/strong>\u76f8\u5173\u7684\u652f\u6301\u65b9\u6cd5\uff08\u9ec4\u8272\u90e8\u5206\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport CoreData\n \n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n \n    var window: UIWindow?\n \n    func application(_ application: UIApplication,\n                     didFinishLaunchingWithOptions\n        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\n        return true\n    }\n \n    func applicationWillResignActive(_ application: UIApplication) {\n    }\n \n    func applicationDidEnterBackground(_ application: UIApplication) {\n    }\n \n    func applicationWillEnterForeground(_ application: UIApplication) {\n    }\n \n    func applicationDidBecomeActive(_ application: UIApplication) {\n \n    }\n \n    func applicationWillTerminate(_ application: UIApplication) {\n    }\n \n    \/\/ MARK: - Core Data stack\n    lazy var persistentContainer: NSPersistentContainer = {\n        let container = NSPersistentContainer(name: \"hangge_1841\")\n        container.loadPersistentStores(completionHandler: { (storeDescription, error) in\n            if let error = error as NSError? {\n                fatalError(\"Unresolved error \\(error), \\(error.userInfo)\")\n            }\n        })\n        return container\n    }()\n     \n    \/\/ MARK: - Core Data Saving support\n    func saveContext () {\n        let context = persistentContainer.viewContext\n        if context.hasChanges {\n            do {\n                try context.save()\n            } catch {\n                let nserror = error as NSError\n                fatalError(\"Unresolved error \\(nserror), \\(nserror.userInfo)\")\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p>\uff084\uff09\u7ecf\u8fc7\u4e0a\u9762\u7684\u914d\u7f6e\u540e\uff0c\u73b0\u5728\u7684\u9879\u76ee\u5c31\u53ef\u4ee5\u4f7f\u7528\u00a0<strong>CoreData\u00a0<\/strong>\u4e86\u3002<strong>CoreData\u00a0<\/strong>\u5177\u4f53\u7528\u6cd5\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u5199\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_767.html\" target=\"_blank\">Swift &#8211; \u4f7f\u7528Core Data\u8fdb\u884c\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\uff0c\u95ee\u9898\u63cf\u8ff0 \uff081\uff09\u4f7f\u7528&nbsp;Core Data&nbsp;\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u3002\u5982\u679c\u9879\u76ee\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3110"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3110"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3110\/revisions"}],"predecessor-version":[{"id":3114,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3110\/revisions\/3114"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}