{"id":6259,"date":"2024-07-10T11:14:08","date_gmt":"2024-07-10T03:14:08","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6259"},"modified":"2024-07-10T11:14:08","modified_gmt":"2024-07-10T03:14:08","slug":"swift-%e4%b8%ad%e7%9a%84-codable-%e5%8d%8f%e8%ae%ae%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6259","title":{"rendered":"Swift \u4e2d\u7684 Codable \u534f\u8bae\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u975e\u539f\u521b\uff0cGemini \u95ee\u7b54\u751f\u6210<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Swift \u4e2d\u7684 Codable \u534f\u8bae\u8be6\u89e3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Codable \u534f\u8bae\u662f Swift 4.0 \u4e2d\u5f15\u5165\u7684\u4e00\u9879\u65b0\u529f\u80fd\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u3001\u7c7b\u578b\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5c06 Swift \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u3001\u679a\u4e3e\u548c\u7c7b\u7f16\u7801\u4e3a JSON \u6570\u636e\uff0c\u6216\u4ece JSON \u6570\u636e\u89e3\u7801\u4e3a Swift \u7c7b\u578b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Codable \u534f\u8bae\u5b9e\u9645\u4e0a\u662f <code class=\"\">Encodable<\/code> \u548c <code class=\"\">Decodable<\/code> \u4e24\u4e2a\u534f\u8bae\u7684\u7ec4\u5408\u3002<code class=\"\">Encodable<\/code> \u534f\u8bae\u5b9a\u4e49\u4e86\u5982\u4f55\u5c06 Swift \u7c7b\u578b\u7f16\u7801\u4e3a JSON \u6570\u636e\uff0c\u800c <code class=\"\">Decodable<\/code> \u534f\u8bae\u5b9a\u4e49\u4e86\u5982\u4f55\u4ece JSON \u6570\u636e\u89e3\u7801\u4e3a Swift \u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u91c7\u7528 Codable \u534f\u8bae\u7684\u4f18\u52bf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u91c7\u7528 Codable \u534f\u8bae\u6709\u4ee5\u4e0b\u51e0\u4e2a\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u6d01:<\/strong> Codable \u534f\u8bae\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u7f16\u7801\u548c\u89e3\u7801\u6570\u636e\uff0c\u65e0\u9700\u7f16\u5199\u7e41\u7410\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u7c7b\u578b\u5b89\u5168:<\/strong> Codable \u534f\u8bae\u53ef\u4ee5\u786e\u4fdd\u7f16\u7801\u548c\u89e3\u7801\u8fc7\u7a0b\u7684\u7c7b\u578b\u5b89\u5168\u6027\uff0c\u9632\u6b62\u51fa\u73b0\u7c7b\u578b\u8f6c\u6362\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6269\u5c55\u6027:<\/strong> Codable \u534f\u8bae\u652f\u6301\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\uff0c\u53ef\u4ee5\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u7f16\u7801\u548c\u89e3\u7801\u9700\u6c42\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u91c7\u7528 Codable \u534f\u8bae\u7684\u6b65\u9aa4<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4f7f Swift \u7c7b\u578b\u7b26\u5408 Codable \u534f\u8bae\uff0c\u53ea\u9700\u5728\u7c7b\u578b\u5b9a\u4e49\u540e\u9762\u6dfb\u52a0 <code class=\"\">Codable<\/code> \u5173\u952e\u5b57\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person: Codable {\n    let name: String\n    let age: Int\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code class=\"\">Person<\/code> \u7ed3\u6784\u4f53\u7b26\u5408 <code class=\"\">Codable<\/code> \u534f\u8bae\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u53ef\u4ee5\u88ab\u7f16\u7801\u4e3a JSON \u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u4ece JSON \u6570\u636e\u89e3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f16\u7801\u548c\u89e3\u7801\u6570\u636e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u7f16\u7801 Swift \u7c7b\u578b\u4e3a JSON \u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code class=\"\">JSONEncoder<\/code> \u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let person = Person(name: \"John Doe\", age: 30)\nlet encoder = JSONEncoder()\ndo {\n    let jsonData = try encoder.encode(person)\n    print(String(data: jsonData, encoding: .utf8)!)\n} catch {\n    print(\"Error encoding person: \\(error)\")\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u5c06 <code class=\"\">Person<\/code> \u5b9e\u4f8b\u7f16\u7801\u4e3a JSON \u6570\u636e\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4ece JSON \u6570\u636e\u89e3\u7801 Swift \u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code class=\"\">JSONDecoder<\/code> \u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let jsonData = Data(\"\"\"\n{\n  \"name\": \"Jane Doe\",\n  \"age\": 25\n}\n\"\"\".utf8)\nlet decoder = JSONDecoder()\ndo {\n    let person = try decoder.decode(Person.self, from: jsonData)\n    print(person)\n} catch {\n    print(\"Error decoding JSON data: \\(error)\")\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u5c06 JSON \u6570\u636e\u89e3\u7801\u4e3a <code class=\"\">Person<\/code> \u5b9e\u4f8b\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u6765\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u7f16\u7801\u548c\u89e3\u7801\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5c06\u65e5\u671f\u7f16\u7801\u4e3a JSON \u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u65e5\u671f\u7f16\u7801\u5668\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u9075\u5faa <code class=\"\">Encoder<\/code> \u534f\u8bae\u7684\u7c7b\u3002<code class=\"\">Encoder<\/code> \u534f\u8bae\u5b9a\u4e49\u4e86\u5982\u4f55\u5c06\u503c\u7f16\u7801\u4e3a JSON \u6570\u636e\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct DateEncoder: Encoder {\n    func container&lt;T&gt;(for type: T) throws -&gt; EncodingContainer&lt;T&gt; where T: Encodable {\n        switch type {\n        case Date.self:\n            return .singleValue(DateContainer(encoder: self))\n        default:\n            return try encoder.container(for: type)\n        }\n    }\n\n    func encode&lt;T&gt;(_ value: T) throws where T: Encodable {\n        if let date = value as? Date {\n            let container = try container(for: Date.self)\n            try container.encode(date)\n        } else {\n            try encoder.encode(value)\n        }\n    }\n}\n\nstruct DateContainer&lt;Encoder&gt;: EncodingContainer {\n    private let encoder: Encoder\n\n    init(encoder: Encoder) {\n        self.encoder = encoder\n    }\n\n    func encode(_ value: Date) throws {\n        let formatter = DateFormatter()\n        formatter.dateFormat = \"yyyy-MM-dd\"\n        let string = formatter.string(from: value)\n        try encoder.encode(string)\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 <code class=\"\">DateEncoder<\/code> \u7c7b\uff0c\u7528\u4e8e\u5c06\u65e5\u671f\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\uff0c\u9700\u8981\u5728\u7f16\u7801\u65f6\u6307\u5b9a\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let person = Person(name: \"John Doe\", age: 30, birthDate: Date())\nlet encoder = JSONEncoder()\nencoder.dateEncodingStrategy = .formatted(DateEncoder())\ndo {\n    let jsonData = try encoder.encode(person)\n    print(String(data: jsonData, encoding: .utf8)!)\n} catch {\n    print(\"Error encoding person: \\(error)\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cGemini \u95ee\u7b54\u751f\u6210 Swift \u4e2d\u7684 Codable \u534f\u8bae\u8be6\u89e3 Codable \u534f\u8bae\u662f Swift&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6259\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[111,67],"class_list":["post-6259","post","type-post","status-publish","format-standard","hentry","category-5","tag-codable","tag-swift"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6259"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6259\/revisions"}],"predecessor-version":[{"id":6260,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6259\/revisions\/6260"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}