--------------------- | Identifiable List | --------------------- | * Item 1 | | * Item 2 | | * Item 3 | ---------------------
Identifiable protocol in iOS Swift - Mini App: Build & Ship
import SwiftUI struct Item: Identifiable { // TODO: Add id and name properties } struct IdentifiableListView: View { let items = [ // TODO: Create some Item instances ] var body: some View { NavigationView { List { // TODO: Show item names here } .navigationTitle("Identifiable List") } } } struct IdentifiableListView_Previews: PreviewProvider { static var previews: some View { IdentifiableListView() } }
import SwiftUI struct Item: Identifiable { let id = UUID() let name: String } struct IdentifiableListView: View { let items = [ Item(name: "Item 1"), Item(name: "Item 2"), Item(name: "Item 3") ] var body: some View { NavigationView { List(items) { item in Text(item.name) } .navigationTitle("Identifiable List") } } } struct IdentifiableListView_Previews: PreviewProvider { static var previews: some View { IdentifiableListView() } }
We created a struct Item that conforms to Identifiable. This means each item must have a unique id. We used UUID() to generate a unique id automatically. The name property holds the text to show.
In the view, we made a list of Item objects. The SwiftUI List uses the id property to track each row uniquely. This helps SwiftUI update the list efficiently when data changes.
The UI shows a simple list with the item names and a navigation title.
--------------------- | Identifiable List | --------------------- | * Item 1 | | * Item 2 | | * Item 3 | ---------------------
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
