{"id":61797,"date":"2024-10-13T13:48:18","date_gmt":"2024-10-13T05:48:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61797\/"},"modified":"2024-10-13T13:48:18","modified_gmt":"2024-10-13T05:48:18","slug":"ios%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%98%e5%84%b2%e9%a1%9e%e5%9e%8b%e5%8f%8a%e5%85%b6%e5%8d%80%e5%88%a5-ios%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%98%e5%84%b2%e9%a1%9e%e5%9e%8b%e7%9a%84%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61797\/","title":{"rendered":"iOS\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u53ca\u5176\u5340\u5225 (ios\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u7684\u6578\u64da\u5eab\u4e2d)"},"content":{"rendered":"<h1 id=\"ios%e6%95%b8%e6%93%9a%e5%ba%ab%e5%ad%98%e5%84%b2%e9%a1%9e%e5%9e%8b%e5%8f%8a%e5%85%b6%e5%8d%80%e5%88%a5-tOWbfkYoYU\">iOS\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u53ca\u5176\u5340\u5225<\/h1>\n<p>\u5728iOS\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9078\u64c7\u5c0d\u65bc\u61c9\u7528\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002iOS\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\uff0c\u6bcf\u7a2e\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u548c\u512a\u7f3a\u9ede\u3002\u672c\u6587\u5c07\u63a2\u8a0eiOS\u4e2d\u5e38\u898b\u7684\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u53ca\u5176\u5340\u5225\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u9078\u64c7\u6700\u9069\u5408\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"1-userdefaults-tOWbfkYoYU\">1. UserDefaults<\/h2>\n<p>UserDefaults\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u5b58\u5132\u65b9\u5f0f\uff0c\u4e3b\u8981\u7528\u65bc\u5b58\u5132\u5c0f\u578b\u7684\u61c9\u7528\u8a2d\u7f6e\u548c\u7528\u6236\u504f\u597d\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u9069\u5408\u5b58\u5132\u57fa\u672c\u6578\u64da\u985e\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6578\u3001\u5e03\u723e\u503c\u7b49\u3002<\/p>\n<pre><code>let defaults = UserDefaults.standard\ndefaults.set(\"Hello, World!\", forKey: \"greeting\")\nlet greeting = defaults.string(forKey: \"greeting\")<\/code><\/pre>\n<p>\u512a\u9ede\uff1a<\/p>\n<ul>\n<li>\u7c21\u55ae\u6613\u7528\uff0c\u9069\u5408\u5c0f\u578b\u6578\u64da\u5b58\u5132\u3002<\/li>\n<li>\u81ea\u52d5\u6301\u4e45\u5316\uff0c\u7121\u9700\u624b\u52d5\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u9ede\uff1a<\/p>\n<ul>\n<li>\u4e0d\u9069\u5408\u5b58\u5132\u5927\u91cf\u6578\u64da\u3002<\/li>\n<li>\u6578\u64da\u7d50\u69cb\u6709\u9650\uff0c\u7121\u6cd5\u5b58\u5132\u8907\u96dc\u5c0d\u8c61\u3002<\/li>\n<\/ul>\n<h2 id=\"2-%e6%96%87%e4%bb%b6%e7%b3%bb%e7%b5%b1-tOWbfkYoYU\">2. \u6587\u4ef6\u7cfb\u7d71<\/h2>\n<p>iOS\u7684\u6587\u4ef6\u7cfb\u7d71\u5141\u8a31\u958b\u767c\u8005\u5c07\u6578\u64da\u4ee5\u6587\u4ef6\u7684\u5f62\u5f0f\u5b58\u5132\u5728\u8a2d\u5099\u4e0a\u3002\u9019\u7a2e\u65b9\u6cd5\u9069\u5408\u5b58\u5132\u5927\u578b\u6578\u64da\uff0c\u5982\u5716\u7247\u3001\u97f3\u983b\u6216\u8996\u983b\u6587\u4ef6\u3002<\/p>\n<pre><code>let filePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!.appending(\"\/myFile.txt\")\ntry? \"Hello, World!\".write(to: URL(fileURLWithPath: filePath), atomically: true, encoding: .utf8)<\/code><\/pre>\n<p>\u512a\u9ede\uff1a<\/p>\n<ul>\n<li>\u9069\u5408\u5b58\u5132\u5927\u578b\u6587\u4ef6\u3002<\/li>\n<li>\u9748\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6587\u4ef6\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u9ede\uff1a<\/p>\n<ul>\n<li>\u9700\u8981\u624b\u52d5\u7ba1\u7406\u6587\u4ef6\u7684\u8b80\u5beb\u3002<\/li>\n<li>\u4e0d\u9069\u5408\u7d50\u69cb\u5316\u6578\u64da\u7684\u5b58\u5132\u3002<\/li>\n<\/ul>\n<h2 id=\"3-sqlite-tOWbfkYoYU\">3. SQLite<\/h2>\n<p>SQLite\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0ciOS\u5167\u5efa\u652f\u6301\u3002\u5b83\u9069\u5408\u5b58\u5132\u7d50\u69cb\u5316\u6578\u64da\uff0c\u4e26\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u67e5\u8a62\u529f\u80fd\u3002<\/p>\n<pre><code>import SQLite3\n\nvar db: OpaquePointer?\nif sqlite3_open(\"myDatabase.sqlite\", &amp;db) == SQLITE_OK {\n    let createTableQuery = \"CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY, name TEXT)\"\n    sqlite3_exec(db, createTableQuery, nil, nil, nil)\n}<\/code><\/pre>\n<p>\u512a\u9ede\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u8907\u96dc\u67e5\u8a62\u548c\u6578\u64da\u95dc\u806f\u3002<\/li>\n<li>\u9069\u5408\u5b58\u5132\u5927\u91cf\u7d50\u69cb\u5316\u6578\u64da\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u9ede\uff1a<\/p>\n<ul>\n<li>\u5b78\u7fd2\u66f2\u7dda\u76f8\u5c0d\u8f03\u9661\u3002<\/li>\n<li>\u9700\u8981\u624b\u52d5\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u548c\u67e5\u8a62\u3002<\/li>\n<\/ul>\n<h2 id=\"4-core-data-tOWbfkYoYU\">4. Core Data<\/h2>\n<p>Core Data\u662fApple\u63d0\u4f9b\u7684\u4e00\u500b\u9ad8\u7d1a\u6578\u64da\u7ba1\u7406\u6846\u67b6\uff0c\u9069\u5408\u7528\u65bc\u5927\u578b\u61c9\u7528\u7684\u6578\u64da\u5b58\u5132\u3002\u5b83\u652f\u6301\u5c0d\u8c61\u5716\u7684\u7ba1\u7406\uff0c\u4e26\u63d0\u4f9b\u4e86\u6578\u64da\u6301\u4e45\u5316\u7684\u529f\u80fd\u3002<\/p>\n<pre><code>import CoreData\n\nlet appDelegate = UIApplication.shared.delegate as! AppDelegate\nlet context = appDelegate.persistentContainer.viewContext\nlet user = User(context: context)\nuser.name = \"John Doe\"\ntry? context.save()<\/code><\/pre>\n<p>\u512a\u9ede\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u5c0d\u8c61\u95dc\u806f\u548c\u6578\u64da\u6a21\u578b\u7684\u7ba1\u7406\u3002<\/li>\n<li>\u81ea\u52d5\u8655\u7406\u6578\u64da\u6301\u4e45\u5316\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u9ede\uff1a<\/p>\n<ul>\n<li>\u76f8\u5c0d\u8907\u96dc\uff0c\u5b78\u7fd2\u6210\u672c\u8f03\u9ad8\u3002<\/li>\n<li>\u6027\u80fd\u53ef\u80fd\u53d7\u5230\u5f71\u97ff\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tOWbfkYoYU\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u9078\u64c7iOS\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u548c\u6578\u64da\u7684\u7279\u6027\u4f86\u505a\u51fa\u6c7a\u7b56\u3002UserDefaults\u9069\u5408\u5c0f\u578b\u6578\u64da\uff0c\u6587\u4ef6\u7cfb\u7d71\u9069\u5408\u5927\u578b\u6587\u4ef6\uff0cSQLite\u5247\u9069\u5408\u7d50\u69cb\u5316\u6578\u64da\uff0c\u800cCore Data\u5247\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7d1a\u7684\u6578\u64da\u7ba1\u7406\u529f\u80fd\u3002\u4e86\u89e3\u9019\u4e9b\u5b58\u5132\u985e\u578b\u7684\u7279\u9ede\u548c\u5340\u5225\uff0c\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728iOS\u61c9\u7528\u958b\u767c\u4e2d\u505a\u51fa\u66f4\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u548c<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22iOS\u6578\u64da\u5eab\u5b58\u5132\u985e\u578b\u53ca\u5176\u5340\u5225\uff0c\u4e86\u89e3SQLite\u3001Core Data\u548cRealm\u7684\u7279\u9ede\u8207\u61c9\u7528\u5834\u666f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61797","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61797","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=61797"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61797\/revisions"}],"predecessor-version":[{"id":61798,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61797\/revisions\/61798"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}