0
0
iOS Swiftmobile~10 mins

View protocol and body property in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AView
BUIView
CViewController
DScene
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View, which is from UIKit, not SwiftUI.
Confusing ViewController with View protocol.
2fill in blank
medium

Complete 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'
AString
BUIView
CViewController
Dsome View
Attempts:
3 left
💡 Hint
Common Mistakes
Returning String instead of a View.
Using UIKit classes like UIView or ViewController.
3fill in blank
hard

Fix 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'
Asome View
BString
CUIView
DViewController
Attempts:
3 left
💡 Hint
Common Mistakes
Using String as the return type of body.
Confusing UIKit classes with SwiftUI views.
4fill in blank
hard

Fill 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'
AView
Bsome View
CUIView
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View protocol.
Returning String instead of some View.
5fill in blank
hard

Fill 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'
AView
Bsome View
C"Welcome to SwiftUI"
DUIView
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIView instead of View protocol.
Not using a string literal inside Text.