0
0
iOS Swiftmobile~10 mins

List with ForEach in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a list showing numbers from 1 to 5 using ForEach.

iOS Swift
List {
  ForEach(1...5, id: \.[1]) { number in
    Text("Number \(number)")
  }
}
Drag options to blanks, or click blank then click option'
Aself
Bid
Cnumber
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined as id.
Leaving out the id parameter causing errors.
2fill in blank
medium

Complete the code to display a list of fruit names using ForEach.

iOS Swift
let fruits = ["Apple", "Banana", "Cherry"]

List {
  ForEach(fruits, id: \.[1]) { fruit in
    Text(fruit)
  }
}
Drag options to blanks, or click blank then click option'
Aindex
Bfruit
Cself
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined as id.
Omitting the id parameter causing errors.
3fill in blank
hard

Fix the error in the ForEach loop by completing the id parameter correctly.

iOS Swift
struct ContentView: View {
  let names = ["Anna", "Bob", "Cara"]

  var body: some View {
    List {
      ForEach(names, id: \.[1]) { name in
        Text(name)
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aself
Bname
Cid
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined.
Using id or index which are not valid here.
4fill in blank
hard

Fill both blanks to create a list of numbers squared using ForEach.

iOS Swift
List {
  ForEach(1...3, id: \.[1]) { num in
    Text("Square: \(num [2] num)")
  }
}
Drag options to blanks, or click blank then click option'
Aself
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using a wrong id parameter.
5fill in blank
hard

Fill all three blanks to create a list showing names in uppercase using ForEach.

iOS Swift
let names = ["alice", "bob", "carol"]

List {
  ForEach(names, id: \.[1]) { name in
    Text(name.[2]().[3]())
  }
}
Drag options to blanks, or click blank then click option'
Aself
Buppercased
CtrimmingCharacters
Dlowercased
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercased() instead of uppercased().
Forgetting to call the method with parentheses.