Pick the fix
func badge() -> some View { if isNew { Text("New") } else { Image(systemName: "star") } } doesn't compile. Best fix?
Hint 1 of 1
Opaque types need one concrete type. What combines branches?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Mark the function @ViewBuilder
Correct. The builder combines both branches into one concrete type.
Why not the others?
- B Wrap both branches in AnyView. It compiles, but it erases types for no reason.
- C Return EmptyView() in the else branch. The branches would still have different types.
- D Change the return type to
any View.any Viewisn't a View that SwiftUI can render here.