What is the purpose of the Identifiable protocol in Swift?
The Identifiable protocol helps uniquely identify instances of a type using an id property. This is useful for lists and UI updates where each item needs a stable identity.
Click to reveal answer
beginner
Which property must a type conforming to Identifiable provide?
A type conforming to Identifiable must provide a property called id that uniquely identifies the instance. The type of id is usually UUID or another unique value type.
Click to reveal answer
intermediate
How does SwiftUI use the Identifiable protocol?
SwiftUI uses Identifiable to track and differentiate views in lists or collections. It helps SwiftUI know which views changed, moved, or were removed to update the UI efficiently.
Click to reveal answer
intermediate
Can the id property be any type in Identifiable?
Yes, the id property can be any type that conforms to Hashable. Common choices are UUID, Int, or String.
Click to reveal answer
beginner
Example: How to make a struct Person conform to Identifiable?
struct Person: Identifiable {
var id = UUID()
var name: String
}
This gives each Person a unique id automatically.
Click to reveal answer
What does the Identifiable protocol require?
AAn <code>id</code> property that uniquely identifies the instance
BA method called <code>identify()</code>
CA static property called <code>id</code>
DNo requirements, it's a marker protocol
✗ Incorrect
The Identifiable protocol requires an id property that uniquely identifies each instance.
Which type is commonly used for the id property in Identifiable?
AString
BDouble
CBool
DUUID
✗ Incorrect
UUID is commonly used because it generates unique identifiers automatically.
Why is Identifiable important in SwiftUI lists?
ATo style the list items
BTo sort the list alphabetically
CTo uniquely track each item for efficient UI updates
DTo add animations automatically
✗ Incorrect
SwiftUI uses Identifiable to track which items changed and update the UI efficiently.
Can the id property be a custom type?
ANo, it must be UUID
BYes, if it conforms to Hashable
CNo, it must be Int
DYes, but only String or Int
✗ Incorrect
The id property can be any type that conforms to Hashable.
What happens if two instances have the same id in an Identifiable collection?
ASwiftUI treats them as the same item
BSwiftUI crashes
CThey are treated as different items
DThe app ignores the <code>id</code> property
✗ Incorrect
If two items share the same id, SwiftUI treats them as the same item, which can cause UI issues.
Explain how the Identifiable protocol helps SwiftUI manage lists.
Think about how SwiftUI knows which item changed in a list.
You got /4 concepts.
Describe how to make a custom struct conform to Identifiable and why you would do it.
Focus on the required property and its purpose.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of the Identifiable protocol in Swift?
easy
A. To enforce a specific initializer
B. To make a class inherit from another class
C. To add default methods to a struct
D. To require a unique id property for each instance
Solution
Step 1: Understand the Identifiable protocol
The Identifiable protocol requires that a type has a unique id property.
Step 2: Identify the purpose of the id
This unique id helps SwiftUI distinguish between different instances, especially in lists.
Final Answer:
To require a unique id property for each instance -> Option D
Quick Check:
Identifiable = unique id required [OK]
Hint: Identifiable means each item must have a unique id [OK]
Common Mistakes:
Thinking it adds methods instead of requiring an id
Confusing it with inheritance
Assuming it enforces initializers
2.
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
}
easy
A. let id: UUID = UUID()
B. var id: UUID
C. var id = UUID()
D. func id() -> UUID
Solution
Step 1: Check property declaration for id
The id must be a property, not a function.
Step 2: Choose correct syntax with initialization
Using let id: UUID = UUID() declares a constant id initialized with a new UUID, which is correct.
Final Answer:
let id: UUID = UUID() -> Option A
Quick Check:
Property with UUID value, not function [OK]
Hint: id must be a property, not a function [OK]
Common Mistakes:
Declaring id as a function
Not initializing id
Using var without initialization
3.
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)
medium
A. 1
B. Anna
C. 2
D. Ben
Solution
Step 1: Identify the second user in the array
Index 1 refers to the second element, which is User(id: 2, name: "Ben").
Step 2: Access the id property
The id of this user is 2, so print(users[1].id) prints 2.
Final Answer:
2 -> Option C
Quick Check:
Second user's id = 2 [OK]
Hint: Array index 1 is second element, check its id [OK]
Common Mistakes:
Confusing index 0 and 1
Printing name instead of id
Assuming id is name
4.
What is wrong with this struct conforming to Identifiable?
struct Product: Identifiable {
let name: String
}
medium
A. Missing id property required by Identifiable
B. The name property should be var, not let
C. Structs cannot conform to protocols
D. The struct must inherit from a class
Solution
Step 1: Check protocol requirements
The Identifiable protocol requires a unique id property.
Step 2: Verify struct properties
This struct only has name, no id property is declared.
Final Answer:
Missing id property required by Identifiable -> Option A
Quick Check:
Identifiable needs id property [OK]
Hint: Identifiable always needs an id property [OK]
Common Mistakes:
Thinking name property suffices
Believing structs can't conform to protocols
Assuming var vs let matters here
5.
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
}
}
hard
A. self.title = title
self.id = title
B. self.title = title
self.id = UUID().uuidString
C. self.title = title
self.id = String()
D. self.title = title
self.id = nil
Solution
Step 1: Understand the need for unique string id
The id must be unique for each task and of type String.
Step 2: Check each option for uniqueness and type
self.title = title
self.id = UUID().uuidString uses UUID().uuidString which generates a unique string each time, fulfilling the requirement.
Final Answer:
self.title = title
self.id = UUID().uuidString -> Option B
Quick Check:
Unique string id = UUID().uuidString [OK]
Hint: Use UUID().uuidString for unique string ids [OK]
Common Mistakes:
Using title as id which may not be unique
Assigning empty string as id
Setting id to nil which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.