{"id":188566,"date":"2024-11-11T23:51:26","date_gmt":"2024-11-11T15:51:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188566\/"},"modified":"2024-11-11T23:51:28","modified_gmt":"2024-11-11T15:51:28","slug":"%e6%8e%a2%e7%b4%a2iphone%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%9a%84%e5%a5%a7%e7%a7%98-iphone%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188566\/","title":{"rendered":"\u63a2\u7d22iPhone\u6578\u64da\u5eab\u61c9\u7528\u7684\u5967\u79d8 (iphone\u6578\u64da\u5eab\u61c9\u7528)"},"content":{"rendered":"<h1 id=\"%e6%8e%a2%e7%b4%a2iphone%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%9a%84%e5%a5%a7%e7%a7%98-QRAewsWZqZ\">\u63a2\u7d22iPhone\u6578\u64da\u5eab\u61c9\u7528\u7684\u5967\u79d8<\/h1>\n<p>\u96a8\u8457\u667a\u80fd\u624b\u6a5f\u7684\u666e\u53ca\uff0ciPhone\u5df2\u6210\u70ba\u4eba\u5011\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u96a8\u4e4b\u800c\u4f86\u7684\u662f\u5c0d\u65bc\u6578\u64da\u7ba1\u7406\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eiPhone\u6578\u64da\u5eab\u61c9\u7528\u7684\u5967\u79d8\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728iOS\u5e73\u53f0\u4e0a\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<h2 id=\"iphone%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-QRAewsWZqZ\">iPhone\u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728iPhone\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u4e3b\u8981\u7528\u65bc\u5b58\u5132\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u6578\u64da\u3002iOS\u5e73\u53f0\u4e0a\u5e38\u7528\u7684\u6578\u64da\u5eab\u6709SQLite\u3001Core Data\u548cRealm\u7b49\u3002\u9019\u4e9b\u6578\u64da\u5eab\u5404\u6709\u7279\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002<\/p>\n<h3 id=\"sqlite-QRAewsWZqZ\">SQLite<\/h3>\n<p>SQLite\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bciOS\u958b\u767c\u4e2d\u3002\u5b83\u7684\u512a\u9ede\u5728\u65bc\u7c21\u55ae\u6613\u7528\uff0c\u4e26\u4e14\u4e0d\u9700\u8981\u984d\u5916\u7684\u4f3a\u670d\u5668\u652f\u6301\u3002\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u5728\u61c9\u7528\u4e2d\u5d4c\u5165SQLite\u6578\u64da\u5eab\uff0c\u4e26\u4f7f\u7528SQL\u8a9e\u53e5\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<pre><code>let db = try Connection(\"path\/to\/db.sqlite3\")\nlet users = Table(\"users\")\nlet id = Expression(\"id\")\nlet name = Expression(\"name\")\n\ntry db.run(users.create { t in\n    t.column(id, primaryKey: .autoincrement)\n    t.column(name)\n})\n<\/code><\/pre>\n<h3 id=\"core-data-QRAewsWZqZ\">Core Data<\/h3>\n<p>Core Data\u662fApple\u63d0\u4f9b\u7684\u4e00\u500b\u6846\u67b6\uff0c\u7528\u65bc\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u6578\u64da\u6a21\u578b\u3002\u5b83\u4e0d\u50c5\u652f\u6301\u6578\u64da\u7684\u6301\u4e45\u5316\uff0c\u9084\u63d0\u4f9b\u4e86\u6578\u64da\u7684\u67e5\u8a62\u548c\u95dc\u806f\u7ba1\u7406\u529f\u80fd\u3002Core Data\u9069\u5408\u9700\u8981\u8907\u96dc\u6578\u64da\u7d50\u69cb\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<pre><code>let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext\nlet newUser = User(context: context)\nnewUser.name = \"John Doe\"\n\ndo {\n    try context.save()\n} catch {\n    print(\"Failed saving\")\n}\n<\/code><\/pre>\n<h3 id=\"realm-QRAewsWZqZ\">Realm<\/h3>\n<p>Realm\u662f\u4e00\u500b\u73fe\u4ee3\u5316\u7684\u79fb\u52d5\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u67e5\u8a62\u529f\u80fd\u3002\u5b83\u7684\u512a\u52e2\u5728\u65bc\u6027\u80fd\u512a\u8d8a\uff0c\u4e26\u4e14\u652f\u6301\u591a\u7a2e\u5e73\u53f0\u3002Realm\u7684API\u8a2d\u8a08\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<pre><code>let realm = try! Realm()\nlet user = User()\nuser.name = \"Jane Doe\"\n\ntry! realm.write {\n    realm.add(user)\n}\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%81%b8%e6%93%87%e8%88%87%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-QRAewsWZqZ\">\u6578\u64da\u5eab\u7684\u9078\u64c7\u8207\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u5728\u9078\u64c7\u6578\u64da\u5eab\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u61c9\u7528\u7684\u9700\u6c42\u3001\u6578\u64da\u7684\u8907\u96dc\u6027\u4ee5\u53ca\u6027\u80fd\u8981\u6c42\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc\u4e00\u500b\u7c21\u55ae\u7684\u5f85\u8fa6\u4e8b\u9805\u61c9\u7528\uff0cSQLite\u53ef\u80fd\u5c31\u8db3\u5920\u4e86\uff1b\u800c\u5c0d\u65bc\u4e00\u500b\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u548c\u8907\u96dc\u67e5\u8a62\u7684\u793e\u4ea4\u5a92\u9ad4\u61c9\u7528\uff0cCore Data\u6216Realm\u5247\u66f4\u70ba\u5408\u9069\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96-QRAewsWZqZ\">\u6578\u64da\u5eab\u7684\u6027\u80fd\u512a\u5316<\/h2>\n<p>\u7121\u8ad6\u9078\u64c7\u54ea\u7a2e\u6578\u64da\u5eab\uff0c\u6027\u80fd\u512a\u5316\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u958b\u767c\u8005\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u5f0f\u4f86\u63d0\u5347\u6578\u64da\u5eab\u7684\u6027\u80fd\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u7d22\u5f15\uff1a\u70ba\u5e38\u7528\u7684\u67e5\u8a62\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u901f\u5ea6\u3002<\/li>\n<li>\u6279\u91cf\u64cd\u4f5c\uff1a\u5728\u9032\u884c\u5927\u91cf\u6578\u64da\u63d2\u5165\u6216\u66f4\u65b0\u6642\uff0c\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u53ef\u4ee5\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u958b\u92b7\u3002<\/li>\n<li>\u5408\u7406\u8a2d\u8a08\u6578\u64da\u6a21\u578b\uff1a\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u5197\u9918\uff0c\u5408\u7406\u8a2d\u8a08\u6578\u64da\u7d50\u69cb\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-QRAewsWZqZ\">\u7d50\u8ad6<\/h2>\n<p>iPhone\u6578\u64da\u5eab\u61c9\u7528\u7684\u958b\u767c\u662f\u4e00\u500b\u8907\u96dc\u4f46\u5145\u6eff\u6311\u6230\u7684\u904e\u7a0b\u3002\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3001\u5408\u7406\u8a2d\u8a08\u6578\u64da\u6a21\u578b\u4ee5\u53ca\u9032\u884c\u6027\u80fd\u512a\u5316\uff0c\u90fd\u662f\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u7684\u91cd\u8981\u56e0\u7d20\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u672a\u4f86\u7684\u6578\u64da\u5eab\u61c9\u7528\u5c07\u6703\u66f4\u52a0\u9ad8\u6548\u548c\u9748\u6d3b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728iOS\u958b\u767c\u4e2d\u4f7f\u7528\u6578\u64da\u5eab\u6709\u9032\u4e00\u6b65\u7684\u8208\u8da3\uff0c\u6216\u8005\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u3001<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u548c<code><a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22iPhone\u6578\u64da\u5eab\u61c9\u7528\u7684\u5967\u79d8\uff0c\u4e86\u89e3\u5982\u4f55\u9ad8\u6548\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u66f4\u667a\u80fd\u7684\u624b\u6a5f\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-188566","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=188566"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188566\/revisions"}],"predecessor-version":[{"id":188567,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188566\/revisions\/188567"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}