The Identifiable protocol helps us give each item a unique ID. This makes it easy to find, update, or show items in lists.
Identifiable protocol in iOS Swift
struct MyItem: Identifiable {
var id = UUID()
var name: String
}The id property must be unique for each instance.
You can use any type for id as long as it conforms to Hashable, but UUID is common.
id is an integer that uniquely identifies each user.struct User: Identifiable {
var id: Int
var username: String
}UUID() to automatically create a unique ID.struct Product: Identifiable {
var id = UUID()
var title: String
}String as the unique ID.struct Task: Identifiable {
let id: String
let description: String
}This SwiftUI view shows a list of fruits. Each fruit is an Item with a unique id. SwiftUI uses the id to track each row.
import SwiftUI struct Item: Identifiable { var id = UUID() var name: String } struct ContentView: View { let items = [ Item(name: "Apple"), Item(name: "Banana"), Item(name: "Cherry") ] var body: some View { List(items) { item in Text(item.name) } } }
Always make sure id is unique to avoid bugs in lists.
If your data already has a unique property, you can use it as id instead of creating a new one.
Identifiable helps SwiftUI know which views changed, so it updates only those views.
The Identifiable protocol requires a unique id for each item.
It is very useful for lists and dynamic views in SwiftUI.
Use simple types like UUID, Int, or String for the id.
Practice
What is the main purpose of the Identifiable protocol in Swift?
Solution
Step 1: Understand the Identifiable protocol
The Identifiable protocol requires that a type has a uniqueidproperty.Step 2: Identify the purpose of the
This uniqueididhelps SwiftUI distinguish between different instances, especially in lists.Final Answer:
To require a uniqueidproperty for each instance -> Option DQuick Check:
Identifiable = unique id required [OK]
- Thinking it adds methods instead of requiring an id
- Confusing it with inheritance
- Assuming it enforces initializers
Which of the following is the correct way to declare a struct conforming to Identifiable with a constant id of type UUID?
struct Item: Identifiable {
// Fill in the blank
}Solution
Step 1: Check property declaration for
Theididmust be a property, not a function.Step 2: Choose correct syntax with initialization
Usinglet id: UUID = UUID()declares a constantidinitialized with a new UUID, which is correct.Final Answer:
let id: UUID = UUID() -> Option AQuick Check:
Property with UUID value, not function [OK]
- Declaring id as a function
- Not initializing id
- Using var without initialization
Given the code below, what will be printed?
struct User: Identifiable {
let id: Int
let name: String
}
let users = [User(id: 1, name: "Anna"), User(id: 2, name: "Ben")]
print(users[1].id)Solution
Step 1: Identify the second user in the array
Index 1 refers to the second element, which isUser(id: 2, name: "Ben").Step 2: Access the
Theidpropertyidof this user is 2, soprint(users[1].id)prints 2.Final Answer:
2 -> Option CQuick Check:
Second user's id = 2 [OK]
- Confusing index 0 and 1
- Printing name instead of id
- Assuming id is name
What is wrong with this struct conforming to Identifiable?
struct Product: Identifiable {
let name: String
}Solution
Step 1: Check protocol requirements
The Identifiable protocol requires a uniqueidproperty.Step 2: Verify struct properties
This struct only hasname, noidproperty is declared.Final Answer:
Missingidproperty required by Identifiable -> Option AQuick Check:
Identifiable needs id property [OK]
- Thinking name property suffices
- Believing structs can't conform to protocols
- Assuming var vs let matters here
You want to create a list of tasks where each task has a unique string identifier. Which of the following struct definitions correctly conforms to Identifiable and uses a string id?
struct Task: Identifiable {
let id: String
let title: String
init(title: String) {
// Fill in the blank
}
}Solution
Step 1: Understand the need for unique string id
Theidmust be unique for each task and of typeString.Step 2: Check each option for uniqueness and type
self.title = title self.id = UUID().uuidString usesUUID().uuidStringwhich generates a unique string each time, fulfilling the requirement.Final Answer:
self.title = title self.id = UUID().uuidString -> Option BQuick Check:
Unique string id = UUID().uuidString [OK]
- Using title as id which may not be unique
- Assigning empty string as id
- Setting id to nil which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
