Bird
Raised Fist0
iOS Swiftmobile~20 mins

Identifiable protocol in iOS Swift - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Identifiable List Screen
This screen shows a list of simple items that conform to the Identifiable protocol. Each item has a unique id and a name. The list displays the names. This helps SwiftUI know which item is which when updating the list.
Target UI
---------------------
| Identifiable List |
---------------------
| * Item 1          |
| * Item 2          |
| * Item 3          |
---------------------
Create a struct named Item that conforms to Identifiable.
Give Item a unique id property of type UUID.
Give Item a name property of type String.
Create a SwiftUI List that shows the names of several Item instances.
Use the id property automatically for the List to identify each row.
Starter Code
iOS Swift
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()
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
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.

Final Result
Completed Screen
---------------------
| Identifiable List |
---------------------
| * Item 1          |
| * Item 2          |
| * Item 3          |
---------------------
User can scroll the list if it grows longer.
Each row shows the item name.
SwiftUI uses the id to keep track of each item for smooth updates.
Stretch Goal
Add the ability to delete items from the list by swiping left on a row.
💡 Hint
Use the .onDelete modifier on the List and make the items array @State to allow changes.

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

  1. Step 1: Understand the Identifiable protocol

    The Identifiable protocol requires that a type has a unique id property.
  2. Step 2: Identify the purpose of the id

    This unique id helps SwiftUI distinguish between different instances, especially in lists.
  3. Final Answer:

    To require a unique id property for each instance -> Option D
  4. 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

  1. Step 1: Check property declaration for id

    The id must be a property, not a function.
  2. Step 2: Choose correct syntax with initialization

    Using let id: UUID = UUID() declares a constant id initialized with a new UUID, which is correct.
  3. Final Answer:

    let id: UUID = UUID() -> Option A
  4. 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

  1. Step 1: Identify the second user in the array

    Index 1 refers to the second element, which is User(id: 2, name: "Ben").
  2. Step 2: Access the id property

    The id of this user is 2, so print(users[1].id) prints 2.
  3. Final Answer:

    2 -> Option C
  4. 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

  1. Step 1: Check protocol requirements

    The Identifiable protocol requires a unique id property.
  2. Step 2: Verify struct properties

    This struct only has name, no id property is declared.
  3. Final Answer:

    Missing id property required by Identifiable -> Option A
  4. 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

  1. Step 1: Understand the need for unique string id

    The id must be unique for each task and of type String.
  2. 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.
  3. Final Answer:

    self.title = title self.id = UUID().uuidString -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard