Bird
0
0

How can you combine a LazyVStack with a conditional view to show a message only when the list is empty?

hard📝 Application Q9 of 15
iOS Swift - SwiftUI Layout
How can you combine a LazyVStack with a conditional view to show a message only when the list is empty?
AUse an if statement inside LazyVStack to conditionally add Text view
BUse a separate VStack outside LazyVStack for the message
CLazyVStack cannot contain conditional views
DUse a ForEach with empty array to show message
Step-by-Step Solution
Solution:
  1. Step 1: Understand SwiftUI view builders

    LazyVStack supports conditional views using if statements inside its closure.
  2. Step 2: Apply conditional logic

    Use if list.isEmpty { Text("No items") } inside LazyVStack to show message only when empty.
  3. Final Answer:

    Use an if statement inside LazyVStack to conditionally add Text view -> Option A
  4. Quick Check:

    Conditional views inside LazyVStack = valid [OK]
Quick Trick: Use if inside LazyVStack for conditional views [OK]
Common Mistakes:
  • Thinking LazyVStack disallows if statements
  • Placing message outside LazyVStack causing layout issues
  • Using ForEach with empty array expecting message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes