{"id":6419,"date":"2024-08-02T10:49:48","date_gmt":"2024-08-02T02:49:48","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6419"},"modified":"2024-08-02T10:50:13","modified_gmt":"2024-08-02T02:50:13","slug":"swift-%e5%9f%ba%e7%a1%80%e5%8d%8f%e8%ae%ae%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6419","title":{"rendered":"Swift \u57fa\u7840\u534f\u8bae\u4ecb\u7ecd"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>Swift \u63d0\u4f9b\u4e86\u591a\u4e2a\u57fa\u7840\u534f\u8bae\uff0c\u8fd9\u4e9b\u534f\u8bae\u5728\u8bb8\u591a\u5e38\u89c1\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\u90fd\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u57fa\u7840\u534f\u8bae\uff0c\u5305\u62ec\u5b83\u4eec\u7684\u4f5c\u7528\u3001\u5982\u4f55\u4f7f\u7528\u4ee5\u53ca\u65b9\u6cd5\u5b9e\u73b0\u7684\u8be6\u7ec6\u4fe1\u606f\uff1a<\/p>\n<h3>1. <code>Equatable<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u5b9a\u4e49\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u7684\u6807\u51c6\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>==<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: Equatable {\n  var name: String\n  var age: Int\n\n  \/\/ \u9ed8\u8ba4\u5b9e\u73b0\u7684 <code>==<\/code> \u8fd0\u7b97\u7b26\u4f1a\u6bd4\u8f83\u6240\u6709\u5c5e\u6027\n}\n\nlet person1 = Person(name: \"Alice\", age: 30)\nlet person2 = Person(name: \"Alice\", age: 30)\nprint(person1 == person2)  \/\/ \u8f93\u51fa: true<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>2. <code>Comparable<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u6392\u5e8f\u89c4\u5219\uff0c\u4f7f\u5bf9\u8c61\u80fd\u591f\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>&lt;<\/code>\uff08\u5c0f\u4e8e\uff09<\/li>\n<li><code>&lt;=<\/code>\uff08\u5c0f\u4e8e\u6216\u7b49\u4e8e\uff09<\/li>\n<li><code>&gt;<\/code>\uff08\u5927\u4e8e\uff09<\/li>\n<li><code>&gt;=<\/code>\uff08\u5927\u4e8e\u6216\u7b49\u4e8e\uff09<\/li>\n<\/ul>\n<p>\u5fc5\u987b\u5b9e\u73b0 <code>&lt;<\/code> \u8fd0\u7b97\u7b26\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7 <code>&lt;<\/code> \u63a8\u5bfc\u5176\u4ed6\u6bd4\u8f83\u64cd\u4f5c\u7b26\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: Comparable {\n  var name: String\n  var age: Int\n\n  static func < (lhs: Person, rhs: Person) -> Bool {\n      return lhs.age < rhs.age\n  }\n\n  \/\/ <code>&lt;=<\/code>, <code>&gt;<\/code>, <code>&gt;=<\/code> \u4f1a\u81ea\u52a8\u901a\u8fc7 <code>&lt;<\/code> \u63a8\u5bfc\n}\n\nlet person1 = Person(name: \"Alice\", age: 30)\nlet person2 = Person(name: \"Bob\", age: 25)\nprint(person1 > person2)  \/\/ \u8f93\u51fa: true<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>3. <code>Hashable<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u751f\u6210\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\uff0c\u4f7f\u5bf9\u8c61\u53ef\u4ee5\u7528\u4f5c\u54c8\u5e0c\u8868\uff08\u5982 <code>Set<\/code> \u548c <code>Dictionary<\/code>\uff09\u7684\u952e\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>hash(into:)<\/code>\uff1a\u5c06\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u5b58\u50a8\u5230\u54c8\u5e0c\u7b97\u6cd5\u4e2d\u3002Swift \u4f1a\u81ea\u52a8\u5408\u6210\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u4f46\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: Hashable {\n  var name: String\n  var age: Int\n\n  func hash(into hasher: inout Hasher) {\n      hasher.combine(name)\n      hasher.combine(age)\n  }\n}\n\nlet person = Person(name: \"Alice\", age: 30)\nlet personSet: Set<Person> = [person]<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>4. <code>CustomStringConvertible<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u63cf\u8ff0\u4fe1\u606f\uff0c\u4f7f\u5bf9\u8c61\u53ef\u4ee5\u4ee5\u66f4\u53cb\u597d\u7684\u65b9\u5f0f\u88ab\u6253\u5370\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>description<\/code> \u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u63cf\u8ff0\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: CustomStringConvertible {\n  var name: String\n  var age: Int\n\n  var description: String {\n      return \"\\(name), \\(age) years old\"\n  }\n}\n\nlet person = Person(name: \"Alice\", age: 30)\nprint(person)  \/\/ \u8f93\u51fa: Alice, 30 years old<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>5. <code>Codable<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u5c06\u5bf9\u8c61\u7f16\u7801\u4e3a\u6216\u89e3\u7801\u81ea JSON \u6216\u5176\u4ed6\u683c\u5f0f\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>encode(to:)<\/code>\uff1a\u5c06\u5bf9\u8c61\u7f16\u7801\u4e3a\u67d0\u79cd\u683c\u5f0f\uff08\u5982 JSON\uff09\u3002<\/li>\n<li><code>init(from:)<\/code>\uff1a\u4ece\u67d0\u79cd\u683c\u5f0f\uff08\u5982 JSON\uff09\u89e3\u7801\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: Codable {\n  var name: String\n  var age: Int\n}\n\n\/\/ \u7f16\u7801\nlet person = Person(name: \"Alice\", age: 30)\nlet encoder = JSONEncoder()\nif let jsonData = try? encoder.encode(person) {\n  print(String(data: jsonData, encoding: .utf8)!)  \/\/ \u8f93\u51fa: {\"name\":\"Alice\",\"age\":30}\n}\n\n\/\/ \u89e3\u7801\nlet decoder = JSONDecoder()\nif let decodedPerson = try? decoder.decode(Person.self, from: jsonData) {\n  print(decodedPerson)  \/\/ \u8f93\u51fa: Person(name: \"Alice\", age: 30)\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>6. <code>Identifiable<\/code><\/h3>\n<ul>\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u7528\u4e8e\u6807\u8bc6\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u4f7f\u5f97\u5bf9\u8c61\u53ef\u4ee5\u5728\u96c6\u5408\u4e2d\u88ab\u552f\u4e00\u8bc6\u522b\u3002<\/li>\n<li>\n<p><strong>\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>id<\/code> \u5c5e\u6027\u63d0\u4f9b\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<pre><code class=\"language-swift\">struct Person: Identifiable {\n  var id: UUID\n  var name: String\n  var age: Int\n}\n\nlet person = Person(id: UUID(), name: \"Alice\", age: 30)<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u57fa\u7840\u534f\u8bae\u4e3a Swift \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u5bf9\u8c61\u7684\u6bd4\u8f83\u3001\u6392\u5e8f\u3001\u54c8\u5e0c\u3001\u63cf\u8ff0\u3001\u7f16\u7801\u548c\u552f\u4e00\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210 Swift \u63d0\u4f9b\u4e86\u591a\u4e2a\u57fa\u7840\u534f\u8bae\uff0c\u8fd9\u4e9b\u534f\u8bae\u5728\u8bb8\u591a\u5e38\u89c1\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\u90fd\u975e\u5e38\u6709\u7528\u3002\u4ee5&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6419\">\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":[135],"tags":[67],"class_list":["post-6419","post","type-post","status-publish","format-standard","hentry","category-135","tag-swift"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6419","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=6419"}],"version-history":[{"count":2,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6419\/revisions"}],"predecessor-version":[{"id":6421,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6419\/revisions\/6421"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}