Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a SwiftUI view conforming to the View protocol.
iOS Swift
struct ContentView: [1] { var body: some View { Text("Hello, world!") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View, which is from UIKit, not SwiftUI.
Confusing ViewController with View protocol.
✗ Incorrect
In SwiftUI, a view must conform to the View protocol to define its UI.
2fill in blank
mediumComplete the code to define the body property returning a Text view.
iOS Swift
var body: [1] { Text("Welcome to SwiftUI") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning String instead of a View.
Using UIKit classes like UIView or ViewController.
✗ Incorrect
The body property must return some View, which is a view type in SwiftUI.
3fill in blank
hardFix the error in the body property declaration to conform to the View protocol.
iOS Swift
var body: [1] { VStack { Text("Line 1") Text("Line 2") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using String as the return type of body.
Confusing UIKit classes with SwiftUI views.
✗ Incorrect
The body property must return some View, not String or UIKit classes.
4fill in blank
hardFill both blanks to complete a SwiftUI view with a VStack containing two Text views.
iOS Swift
struct GreetingView: [1] { var body: [2] { VStack { Text("Hello") Text("SwiftUI") } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View protocol.
Returning String instead of some View.
✗ Incorrect
The struct must conform to View and the body property returns some View.
5fill in blank
hardFill all three blanks to create a SwiftUI view with a body that returns a VStack containing two Text views.
iOS Swift
struct InfoView: [1] { var body: [2] { VStack { Text([3]) Text("SwiftUI Rocks") } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View protocol.
Not using a string literal inside Text.
✗ Incorrect
The struct conforms to View, body returns some View, and Text needs a string literal.