{"id":3115,"date":"2021-06-08T13:07:01","date_gmt":"2021-06-08T05:07:01","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3115"},"modified":"2021-06-08T13:07:01","modified_gmt":"2021-06-08T05:07:01","slug":"swift-%e4%bd%bf%e7%94%a8core-data%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e6%8c%81%e4%b9%85%e5%8c%96%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3115","title":{"rendered":"Swift &#8211; \u4f7f\u7528Core Data\u8fdb\u884c\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8"},"content":{"rendered":"\n<p><strong>\u4e00\uff0cCore Data\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>1\uff0cCore Data\u662fiOS5\u4e4b\u540e\u624d\u51fa\u73b0\u7684\u4e00\u4e2a\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u8c61-\u5173\u7cfb\u6620\u5c04(ORM)\u7684\u529f\u80fd\uff0c\u5373\u80fd\u591f\u5c06\u5bf9\u8c61\u8f6c\u5316\u6210\u6570\u636e\uff0c\u4e5f\u80fd\u591f\u5c06\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fd8\u539f\u6210\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>2\uff0c\u867d\u7136\u5176\u5e95\u5c42\u4e5f\u662f\u7531\u7c7b\u4f3c\u4e8eSQL\u7684\u6280\u672f\u6765\u5b9e\u73b0\uff0c\u4f46\u6211\u4eec\u4e0d\u9700\u8981\u7f16\u5199\u4efb\u4f55SQL\u8bed\u53e5\uff0c\u6709\u70b9\u50cfJava\u5f00\u53d1\u4e2d\u7684Hibernate\u6301\u4e45\u5316\u6846\u67b6<\/p>\n\n\n\n<p>3\uff0cCore Data\u6570\u636e\u6700\u7ec8\u7684\u5b58\u50a8\u7c7b\u578b\u53ef\u4ee5\u662f\uff1aSQLite\u6570\u636e\u5e93\uff0cXML\uff0c\u4e8c\u8fdb\u5236\uff0c\u5185\u5b58\u91cc\uff0c\u6216\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002<br><\/p>\n\n\n\n<p>4\uff0c\u4e0eSQLite\u533a\u522b\uff1a\u53ea\u80fd\u53d6\u51fa\u6574\u4e2a\u5b9e\u4f53\u8bb0\u5f55\uff0c\u7136\u540e\u5206\u89e3\uff0c\u4e4b\u540e\u624d\u80fd\u5f97\u5230\u5b9e\u4f53\u7684\u67d0\u4e2a\u5c5e\u6027\u3002\u00a0<\/p>\n\n\n\n<p><strong>\u4e8c\uff0cCore Data\u7684\u4f7f\u7528\u51c6\u5907 &#8211; \u6570\u636e\u6a21\u578b\u548c\u5b9e\u4f53\u7c7b\u7684\u521b\u5efa<\/strong>1\uff0c\u521b\u5efa\u9879\u76ee\u7684\u65f6\u5019\uff0c\u52fe\u9009\u201c<strong>Use Core Data<\/strong>\u201d\u3002\u5b8c\u6bd5\u540e\u5728\u00a0<strong>AppDelegate<\/strong>\u00a0\u4e2d\uff0c\u4f1a\u751f\u6210\u76f8\u5173\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>1\uff0c\u521b\u5efa\u9879\u76ee\u7684\u65f6\u5019\uff0c\u52fe\u9009\u201c<strong>Use Core Data<\/strong>\u201d\u3002\u5b8c\u6bd5\u540e\u5728\u00a0<strong>AppDelegate<\/strong>\u00a0\u4e2d\uff0c\u4f1a\u751f\u6210\u76f8\u5173\u4ee3\u7801\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-18-1024x610.png\" alt=\"\" class=\"wp-image-3116\" width=\"511\" height=\"304\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-1024x610.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-300x179.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-768x457.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-830x494.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18-480x286.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-18.png 1028w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/figure><\/div>\n\n\n\n<p>2\uff0c\u6253\u5f00\u9879\u76ee\u4e2d\u7684\u00a0<strong>xcdatamodeld<\/strong>\u00a0\u6587\u4ef6\uff0c\u5728\u53f3\u8fb9\u7684\u6570\u636e\u6a21\u578b\u7f16\u8f91\u5668\u7684\u5e95\u90e8\u5de5\u5177\u680f\u70b9\u51fb\u00a0<strong>Add Entity<\/strong>\u00a0\u6dfb\u52a0\u5b9e\u4f53\u3002\u540c\u65f6\u5728\u5c5e\u6027\u680f\u4e2d\u5bf9\u5b9e\u4f53\u547d\u540d\u8fdb\u884c\u4fee\u6539\uff0c\u5e76\u5728\u00a0<strong>Attribute<\/strong>\u00a0\u680f\u76ee\u4e2d\u6dfb\u52a0\u00a0<strong>id<\/strong>\uff0c<strong>username<\/strong>\uff0c<strong>password<\/strong>\u00a0\u4e09\u4e2a\u5c5e\u6027\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-19-1024x509.png\" alt=\"\" class=\"wp-image-3117\" width=\"568\" height=\"282\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-1024x509.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-768x382.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-830x413.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-350x174.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19-480x239.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-19.png 1440w\" sizes=\"(max-width: 568px) 100vw, 568px\" \/><\/figure><\/div>\n\n\n\n<p>3\uff0c\u70b9\u51fb\u4e0b\u65b9\u7684\u00a0<strong>Editor Style<\/strong>\u00a0\u6309\u94ae\u53ef\u4ee5\u67e5\u770b\u5b9e\u4f53\u7684\u5173\u7cfb\u56fe\u3002<br><\/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-20-1024x516.png\" alt=\"\" class=\"wp-image-3118\" width=\"620\" height=\"312\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-1024x516.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-300x151.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-768x387.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-830x418.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-230x116.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-350x176.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20-480x242.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-20.png 1452w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/figure><\/div>\n\n\n\n<p>4\uff0c\u81ea\u00a0<strong>iOS10<\/strong>\u00a0\u548c\u00a0<strong>swift3<\/strong>\u00a0\u4e4b\u540e\uff0c\u8bbf\u95ee\u00a0<strong>CoreData<\/strong>\u00a0\u7684\u65b9\u6cd5\u7b80\u6d01\u4e86\u8bb8\u591a\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u624b\u52a8\u65b0\u5efa\u5bf9\u5e94\u4e8e\u00a0<strong>entity<\/strong>\u00a0\u7684\u00a0<strong>class<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u4e09\uff0cCore Data\u7684\u4f7f\u7528<\/strong><\/p>\n\n\n\n<p><strong>1\uff0c\u9996\u5148\u5728\u4ee3\u7801\u4e2d\u5f15\u5165CoreData\u5e93<\/strong><\/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 CoreData<\/pre>\n\n\n\n<p><strong>2\uff0c\u63d2\u5165\uff08\u4fdd\u5b58\uff09\u6570\u636e\u64cd\u4f5c<\/strong><\/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=\"\">\/\/\u83b7\u53d6\u7ba1\u7406\u7684\u6570\u636e\u4e0a\u4e0b\u6587 \u5bf9\u8c61\nlet app = UIApplication.shared.delegate as! AppDelegate\nlet context = app.persistentContainer.viewContext\n \n\/\/\u521b\u5efaUser\u5bf9\u8c61\nlet user = NSEntityDescription.insertNewObject(forEntityName: \"User\",\n                                               into: context) as! User\n \n\/\/\u5bf9\u8c61\u8d4b\u503c\nuser.id = 1\nuser.username = \"hangge\"\nuser.password = \"1234\"\n \n\/\/\u4fdd\u5b58\ndo {\n    try context.save()\n    print(\"\u4fdd\u5b58\u6210\u529f\uff01\")\n} catch {\n    fatalError(\"\u4e0d\u80fd\u4fdd\u5b58\uff1a\\(error)\")\n}\n<\/pre>\n\n\n\n<p><strong>3\uff0c\u67e5\u8be2\u6570\u636e\u64cd\u4f5c<\/strong><\/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=\"\">\/\/\u83b7\u53d6\u7ba1\u7406\u7684\u6570\u636e\u4e0a\u4e0b\u6587 \u5bf9\u8c61\nlet app = UIApplication.shared.delegate as! AppDelegate\nlet context = app.persistentContainer.viewContext\n \n\/\/\u58f0\u660e\u6570\u636e\u7684\u8bf7\u6c42\nlet fetchRequest = NSFetchRequest&lt;User>(entityName:\"User\")\nfetchRequest.fetchLimit = 10 \/\/\u9650\u5b9a\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u91cf\nfetchRequest.fetchOffset = 0 \/\/\u67e5\u8be2\u7684\u504f\u79fb\u91cf\n \n\/\/\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6\nlet predicate = NSPredicate(format: \"id= '1' \", \"\")\nfetchRequest.predicate = predicate\n \n\/\/\u67e5\u8be2\u64cd\u4f5c\ndo {\n    let fetchedObjects = try context.fetch(fetchRequest)\n     \n    \/\/\u904d\u5386\u67e5\u8be2\u7684\u7ed3\u679c\n    for info in fetchedObjects{\n        print(\"id=\\(info.id)\")\n        print(\"username=\\(info.username)\")\n        print(\"password=\\(info.password)\")\n    }\n}\ncatch {\n    fatalError(\"\u4e0d\u80fd\u4fdd\u5b58\uff1a\\(error)\")\n}\n<\/pre>\n\n\n\n<p><strong>4\uff0c\u4fee\u6539\u6570\u636e\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<p>\u5373\u5c06\u67e5\u8be2\u51fa\u6765\u7684\u5bf9\u8c61\u8fdb\u884c\u91cd\u65b0\u8d4b\u503c\uff0c\u7136\u540e\u518d\u4f7f\u7528context.save\u65b9\u6cd5\u91cd\u65b0\u4fdd\u5b58\u5373\u53ef \u00a0<\/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=\"\">\/\/\u904d\u5386\u67e5\u8be2\u7684\u7ed3\u679c\nfor info in fetchedObjects{\n    \/\/\u4fee\u6539\u5bc6\u7801\n    info.password = \"abcd\"\n    \/\/\u91cd\u65b0\u4fdd\u5b58\n    try context.save()\n}\n<\/pre>\n\n\n\n<p><strong>5\uff0c\u5220\u9664\u6570\u636e\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<p>\u5220\u9664\u64cd\u4f5c\u4f7f\u7528context.delete\u65b9\u6cd5\uff0c\u5220\u9664\u67d0\u4e2a\u5bf9\u8c61\u3002\u7136\u540e\u4f7f\u7528context.save\u65b9\u6cd5\u4fdd\u5b58\u66f4\u65b0\u5230\u6570\u636e\u5e93 \u00a0<\/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=\"\">\/\/\u904d\u5386\u67e5\u8be2\u7684\u7ed3\u679c\nfor info in fetchedObjects{\n    \/\/\u5220\u9664\u5bf9\u8c61\n    context.delete(info)\n}\n \n\/\/\u91cd\u65b0\u4fdd\u5b58-\u66f4\u65b0\u5230\u6570\u636e\u5e93\ntry! context.save()<\/pre>\n\n\n\n<p><strong>\u56db\uff0c\u6570\u636e\u7684\u5b58\u653e\u4f4d\u7f6e\u00a0<\/strong><\/p>\n\n\n\n<p>\u9ed8\u8ba4Core Data\u751f\u6210\u7684\u662fsqlite\u6587\u4ef6\uff0c\u4fdd\u5b58\u5728Documents\u6587\u4ef6\u5939\u4e0b<\/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-21-1024x304.png\" alt=\"\" class=\"wp-image-3119\" width=\"572\" height=\"170\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-1024x304.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-300x89.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-768x228.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-830x246.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-230x68.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-350x104.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21-480x142.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/06\/image-21.png 1194w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\uff0cCore Data\u4ecb\u7ecd 1\uff0cCore Data\u662fiOS5\u4e4b\u540e\u624d\u51fa\u73b0\u7684\u4e00\u4e2a\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u8c61- [&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\/3115"}],"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=3115"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3115\/revisions"}],"predecessor-version":[{"id":3120,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3115\/revisions\/3120"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}