{"id":3551,"date":"2025-12-31T10:50:39","date_gmt":"2025-12-31T01:50:39","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=3551"},"modified":"2025-12-31T12:55:05","modified_gmt":"2025-12-31T03:55:05","slug":"swift-protocols","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/12\/31\/swift-protocols\/","title":{"rendered":"[Swift]Identifiable, Equatable, Hashable Protocols"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc774\uc804 \ud3ec\uc2a4\ud2b8\uc5d0\uc11c randomuser api \ub370\uc774\ud130\ub97c struct\uad6c\uc870\ub85c \ubcc0\ud658\ud574\uc11c \uc571\uc5d0\uc11c \ub370\uc774\ud130\ub97c \ud45c\ud604\ud558\ub3c4\ub85d \uad6c\ud604\ud574\ubd24\uc2b5\ub2c8\ub2e4.<br>In the previous post, I converted the randomuser API data into a struct structure and implemented it to represent the data in the app.<\/p>\n\n\n\n<p>\uc774 struct\uad6c\uc870\uc5d0\uc11c \ub9ce\uc774 \uc0ac\uc6a9\ub418\ub294 \ud504\ub85c\ud1a0\ucf5c\uc774 Identifiable,Equatable,Hashable\uc785\ub2c8\ub2e4.<br>The protocols that are widely used in this struct structure are Identifiable, Equatable, and Hashable.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/12\/25\/swift-infinitescreoo-refresh-loading\/\">https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/12\/25\/swift-infinitescreoo-refresh-loading\/<\/a><\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \uc774 \ud504\ub85c\ud1a0\ucf5c\ub4e4\uc758 \uae30\ub2a5\uc744 \uc774\ud574\ud558\uae30 \uc704\ud55c \ub300\ud45c\uc778 \uae30\ub2a5\ub9cc \uad6c\ud604\ud588\uc2b5\ub2c8\ub2e4.<br>Below, only representative functions are implemented to help you understand the functionality of these protocols.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffbIdentifiable , Hashable<\/p>\n\n\n\n<p>Swift\uc5d0\uc11c \uc704\uc758 \uc608\uc81c\ucc98\ub7fc api\ub370\uc774\ud130\ub97c struct\uad6c\uc870\ub85c \ubcc0\ud658\ud558\uc5ec \uc571\uc5d0\uc11c \uc0ac\uc6a9\ud558\ub294 \uad6c\uc870\ub4f1\uc5d0\uc11c Identifiable,Equatable,Hashable\uc774 \ub9ce\uc774 \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<br>In Swift, Identifiable, Equatable, and Hashable are widely used in structures used in apps by converting API data into struct structures, as in the example above.<\/p>\n\n\n\n<p>struct\ub098 \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9 \ud560\uacbd\uc6b0 \uc778\uc2a4\ud134\uc2a4(\uac1d\uccb4)\ub97c \uc0dd\uc131\ud558\uac8c \ub429\ub2c8\ub2e4.<br>When using a struct or class, an instance (object) is created.<\/p>\n\n\n\n<p>\uc774 \ub54c \uc544\ub798\ucc98\ub7fc \ub3d9\uc77c\ud55c \uac12\uc744 \ub300\uc785\ud558\uc5ec \uc778\uc2a4\ud134\uc2a4\ub97c \uc0dd\uc131\ud558\uba74 \ubc18\ubcf5\ubb38\uc5d0\uc11c \ucd9c\ub825\uc2dc \ubc18\ubcf5\ubb38 \ub0b4\uc5d0\uc11c \uac01 \uc778\uc2a4\ud134\uc2a4\ub97c \uad6c\ubd84\uc9d3\uae30\uac00 \ud798\ub4e4\uc5b4\uc9d1\ub2c8\ub2e4.<br>At this time, if you create an instance by substituting the same value as below, it becomes difficult to distinguish each instance within the loop when outputting it.<\/p>\n\n\n\n<p>\uadf8\ub798\uc11c Identifiable\uc774\ub098,Hashable \ud504\ub85c\ud1a0\ucf5c\uc744 \uc0ac\uc6a9\ud574\uc11c \uac01 \uc778\uc2a4\ud134\uc2a4\ub97c \uad6c\ubd84\uc9c0\uc744 \uc218 \uc788\uac8c \ud574\uc90d\ub2c8\ub2e4.<br>So, we can use the Identifiable or Hashable protocol to distinguish each instance.<\/p>\n\n\n\n<p>Identifiable\uc740 \uc0ac\uc6a9\uc790\uac00 \uc9c1\uc811 id\ub97c \uc0dd\uc131\ud574\uc11c \uad6c\ubd84 \uc9d3\ub294 \ubc29\ubc95\uc774\uace0 Hashable \ud504\ub85c\ud1a0\ucf5c\uc744 \uc0ac\uc6a9\ud558\uba74 \uc790\ub3d9\uc801\uc73c\ub85c \ub0b4\ubd80\uc5d0 hash\uac12\uc774 \uc0dd\uc131\ub418\uc5b4 \uc778\uc2a4\ud134\uc2a4\ub97c \uad6c\ubd84\uc9c0\uc744 \uc218 \uc788\uac8c \ud574\uc90d\ub2c8\ub2e4.<br>Identifiable is a method in which the user creates an id to distinguish them, and if the Hashable protocol is used, a hash value is automatically generated internally to distinguish instances.<\/p>\n\n\n\n<p>\uc778\uc2a4\ud134\uc2a4\ub97c \uad6c\ubd84\uc9c0\uc5b4\uc57c\ud558\ub294 \uc774\uc720\ub294 \ub098\uc911\uc5d0 \ub370\uc774\ud130\uc758 \ucd94\uac00 \uc0ad\uc81c \ubcc0\uacbd\uc774 \ubc1c\uc0dd\ud560\ub54c \ud544\uc694\ud558\uae30 \ub54c\ubb38\uc785\ub2c8\ub2e4.<br>The reason we need to distinguish between instances is because we need to do additional deletions and changes to the data later.<\/p>\n\n\n\n<p>\u2714\ufe0f Identifiable\uc740 \uc774\ub807\uac8c \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<br>Identifiable is used like this:<\/p>\n\n\n\n<p>Identifiable\uc774 \ub9cc\uc871\ub418\uba74  SwiftUI\uc758 List, ForEach \ub4f1\uc5d0\uc11c \ubc14\ub85c \uc0ac\uc6a9 \uac00\ub2a5\ud569\ub2c8\ub2e4.<br>Once Identifiable is satisfied, it can be used directly in SwiftUI&#8217;s List, ForEach, etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person: Identifiable {\n    let id = UUID()  \/\/ \uace0\uc720 UUID \uc790\ub3d9 \uc0dd\uc131\n    let name: String\n    let age: Int\n}<\/code><\/pre>\n\n\n\n<p>List,ForEach\uc5d0\uc11c \uc0ac\uc6a9\ubc29\ubc95<br>How to use List, ForEach<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import SwiftUI\n\nstruct Person: Identifiable {\n\/\/ \uace0\uc720 UUID \uc790\ub3d9 \uc0dd\uc131 \/ Automatically generate unique UUID\n    let id = UUID()          \n    let name: String\n    let age: Int\n}\n\nstruct PersonListView: View {\n    let people: &#91;Person] = &#91;\n        Person(name: \"Alice\", age: 30),\n        Person(name: \"Bob\", age: 35),\n        Person(name: \"Charlie\", age: 25),\n        Person(name: \"Diana\", age: 28) \n    ]\n    \n    var body: some View {\n        List(people) { person in\n            HStack {\n                Text(person.name)\n                    .font(.headline)\n                Spacer()\n                Text(\"\\(person.age) years old\")\n                    .foregroundColor(.secondary)\n            }\n        }\n        .navigationTitle(\"People List\")\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Hashable\uc740 \uc774\ub807\uac8c \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<br>This is how Hashable is used:<\/p>\n\n\n\n<p>Dictionary\uc758 \ud0a4\ub85c \uc0ac\uc6a9\ud560 \uc218 \uc788\uac8c \ud574\uc90d\ub2c8\ub2e4.<br>Allows you to use it as a key in a dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Product: Hashable {\n    let productID: Int\n    let name: String\n}<\/code><\/pre>\n\n\n\n<p>Hashable\uc774 \uc801\uc6a9\ub41c struct\ub294 \ub2e4\uc74c\uacfc \uac19\uc774 \uc0ac\uc6a9\ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>A struct with Hashable applied can be used as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ prices\ub515\uc154\ub108\ub9ac\ub97c Product\uc640 Double\ud0c0\uc785\uc73c\ub85c \uc0dd\uc131\n\/\/ Create a prices dictionary with Product and Double types\nvar prices: &#91;Product: Double] = &#91;:]\n\n\/\/ \uac12\uc774 \uac19\uc740 \uc778\uc2a4\ud134\uc2a41 \/ Instance 1 with the same value\nlet iPhone = Product(productID: 1, name: \"iPhone 15\") \nlet iPad = Product(productID: 2, name: \"iPad Pro\")\n\n\/\/ 1500000\uc640 \uac19\uc740\uac12, \uc5b8\ub354\uc2a4\ucf54\uc5b4\ub294 \ubb34\uc2dc\ub428.\n\/\/ Same value as 1500000, underscores are ignored.\nprices&#91;iPhone] = 1_500_000 \nprices&#91;iPad] = 1_200_000\n\n\/\/ \uac19\uc740 \uc81c\ud488\uc73c\ub85c \uac00\uaca9 \uc870\ud68c \/ Check price for the same product\n\/\/ \uac12\uc774 \uac19\uc740 \uc778\uc2a4\ud134\uc2a42 \/ Instance 2 with the same value\nlet sameiPhone = Product(productID: 1, name: \"iPhone 15\") \nprint(prices&#91;sameiPhone] ?? 0)  \/\/ 1500000.0\n\n\/\/ \uac00\uaca9 \uc5c5\ub370\uc774\ud2b8 \/ price update\nprices&#91;sameiPhone] = 1_450_000  \nprint(prices&#91;iPhone]!)  \/\/ 1450000.0<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb Equatable<\/p>\n\n\n\n<p>\u2714\ufe0f \uc0dd\uc131\ub41c \uc778\uc2a4\ud134\uc2a4(\uac1d\uccb4)\uc758 \ub0b4\uc6a9(\uac12)\uc744 \ube44\uad50\ud558\uae30 \uc704\ud574\uc11c \uc0ac\uc6a9\ub418\ub294 \ud504\ub85c\ud1a0\ucf5c\uc785\ub2c8\ub2e4.<br>This is a protocol used to compare the contents (values) of created instances (objects).<\/p>\n\n\n\n<p>\u2714\ufe0f Equatable\uc740 \ub2e4\uc74c\uacfc \uac19\uc774 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>Equatable is used like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person: Equatable { \n    let name: String\n    let age: Int\n    let email: String\n}\n\nlet p1 = Person(name: \"\uae40\ucca0\uc218\", age: 30, email: \"kim@example.com\")\nlet p2 = Person(name: \"\uae40\ucca0\uc218\", age: 30, email: \"kim@example.com\")\n\nprint(p1 == p2)  \/\/ true \u2190 Swift\uac00 \uc790\ub3d9\uc73c\ub85c \ubaa8\ub4e0 \ud504\ub85c\ud37c\ud2f0 \ube44\uad50\ud574\uc90c<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Swift \ucd08\uae30 \ubc84\uc804(\uc608: Swift 3~4.0) \uc2a4\ud0c0\uc77c\ub85c, Equatable\uc744 \uc9c1\uc811 \uad6c\ud604\ud55c \ud615\ud0dc\uc785\ub2c8\ub2e4.<br>This is a direct implementation of Equatable in the style of early Swift versions (e.g. Swift 3-4.0).<\/p>\n\n\n\n<p>\u2714\ufe0f \uc6d0\ub798\ub294 static func == \uc774\ub807\uac8c == \uc774\ub984\uc778 \ud568\uc218\ub97c \ub9cc\ub4e4\uc5b4 \uc8fc\uace0 == \uc774 \uc5f0\uc0b0\uc790\uac00 \ud638\ucd9c\ub418\uba74 <br>static func == \uc774 \ud568\uc218\uac00 \ud638\ucd9c\ub418\ub294 \ud615\ud0dc\uc785\ub2c8\ub2e4. <br>Originally, a function named == was created like this: static func == , and when the == operator was called,<br>static func == This function was called.<\/p>\n\n\n\n<p>\u2714\ufe0f \ud558\uc9c0\ub9cc swift4.1\ubd80\ud130\ub294 Equtable\ud504\ub85c\ud1a0\ucf5c\uc744 \uc0ac\uc6a9\ud558\uace0 == \uc5f0\uc0b0\uc790\ub9cc \ud638\ucd9c\ud558\uba74 swift\uac00 \uc790\ub3d9\uc73c\ub85c \ud568\uc218\ub97c \ub9cc\ub4e4\uc5b4 \uc90d\ub2c8\ub2e4.<br>However, starting from Swift 4.1, if you use the Equtable protocol and only call the == operator, Swift will automatically create a function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Person: Equatable {\n    let name: String\n    let age: Int\n    let email: String\n    \n    static func == (lhs: Person, rhs: Person) -> Bool {\n        return lhs.name == rhs.name &amp;&amp; lhs.age == rhs.age\n        \/\/ email\uc740 \ube44\uad50\uc5d0 \ud3ec\ud568 \uc548 \ub428\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uacb0\ub860\uc740 identifiable\uacfc Hashable\uc740 \ub3d9\uc77c\ud55c \uac12\uc744 \uac00\uc9c4 \uc778\uc2a4\ud134\uc2a4\ub97c \ubc18\ubcf5\ubb38\uc774\ub098 \ucf5c\ub809\uc158\uc5d0\uc11c \uad6c\ubd84\uc9d3\uae30 \uc704\ud574\uc11c \uc0ac\uc6a9\ub418\ub294 \uac70\uace0 Equatable\uc740 \uc778\uc2a4\ud134\uc2a4\uc758 \uac12\uc744 \uc27d\uac8c \ube44\uad50\ud560 \uc218\uc788\uac8c \ud574\uc8fc\ub294 \ud504\ub85c\ud1a0\ucf5c \uc785\ub2c8\ub2e4.<br>In conclusion, identifiable and Hashable are used to distinguish instances with the same value in loops or collections, and Equatable is a protocol that allows for easy comparison of instance values.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc774\uc804 \ud3ec\uc2a4\ud2b8\uc5d0\uc11c randomuser api \ub370\uc774\ud130\ub97c struct\uad6c\uc870\ub85c \ubcc0\ud658\ud574\uc11c \uc571\uc5d0\uc11c \ub370\uc774\ud130\ub97c \ud45c\ud604\ud558\ub3c4\ub85d \uad6c\ud604\ud574\ubd24\uc2b5\ub2c8\ub2e4.In the previous post, I converted the randomuser API data into a struct structure and implemented it to represent the data in the app. \uc774 struct\uad6c\uc870\uc5d0\uc11c \ub9ce\uc774 \uc0ac\uc6a9\ub418\ub294 \ud504\ub85c\ud1a0\ucf5c\uc774 Identifiable,Equatable,Hashable\uc785\ub2c8\ub2e4.The protocols that are widely used in this struct structure are Identifiable, Equatable, and Hashable. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1],"tags":[],"class_list":["post-3551","post","type-post","status-publish","format-standard","hentry","category-swift","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/3551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=3551"}],"version-history":[{"count":50,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/3551\/revisions"}],"predecessor-version":[{"id":3603,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/3551\/revisions\/3603"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=3551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=3551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=3551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}