Which SwiftUI code snippet correctly implements this using NavigationLink inside a List?
hard📝 Application Q9 of 15
iOS Swift - Navigation
You want to display a list of fruits where tapping each fruit navigates to a detail view showing the fruit's name. Which SwiftUI code snippet correctly implements this using NavigationLink inside a List?
A<pre>List(fruits, id: \.self) { fruit in
NavigationLink(destination: Text(fruit)) {
Text(fruit)
}
}</pre>
B<pre>List(fruits) { fruit in
Text(fruit)
NavigationLink(destination: Text(fruit))
}</pre>
C<pre>ForEach(fruits, id: \.self) { fruit in
NavigationLink(label: Text(fruit), destination: Text(fruit))
}</pre>
D<pre>List(fruits) { fruit in
NavigationLink(destination: Text(fruit))
}</pre>
Step-by-Step Solution
Solution:
Step 1: Use List with identifiable data
Use List(fruits, id: \.self) to iterate over fruit names.
Step 2: Wrap each item in NavigationLink
Each row should be a NavigationLink with destination showing the fruit name and label showing the fruit.
Step 3: Verify syntax correctness
List(fruits, id: \.self) { fruit in
NavigationLink(destination: Text(fruit)) {
Text(fruit)
}
}
correctly uses trailing closure syntax for NavigationLink inside List.
Final Answer:
Option A -> Option A
Quick Check:
NavigationLink inside List with destination and label [OK]
Quick Trick:Use NavigationLink inside List with destination and label closures [OK]
Common Mistakes:
Placing NavigationLink outside List row
Missing label closure in NavigationLink
Incorrect List or ForEach usage
Master "Navigation" in iOS Swift
9 interactive learning modes - each teaches the same concept differently