#17: Multiple Return Types
Question
// Swift Quiz
— objc.io (@objcio) June 12, 2019
struct MyView : View {
@State var loading: Bool = false
var body: some View {
if loading {
return Text("Hello")
} else {
return Button(action: { }) { Text("Tap!") }
}
}
}
// What’s the output?
Solution
Solution: Compilation Error
— objc.io (@objcio) June 13, 2019
This causes a compilation error, because the types of Text and Button are different. Either wrap both views in a Group, or wrap them individually in an AnyView.https://t.co/s7500CWmAg