Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a SwiftUI view struct.
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 UIKit classes like UIViewController instead of SwiftUI View.
✗ Incorrect
In SwiftUI, views are structs that conform to the View protocol.
2fill in blank
mediumComplete the code to create a state variable in SwiftUI.
iOS Swift
@[1] private var count = 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservedObject or @Binding when a simple state is needed.
✗ Incorrect
The @State property wrapper is used to declare a state variable in SwiftUI.
3fill in blank
hardFix the error in the code by completing the missing protocol conformance.
iOS Swift
class DataModel: [1] { @Published var name = "" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Identifiable or Codable which do not support @Published.
✗ Incorrect
To use @Published properties, the class must conform to ObservableObject.
4fill in blank
hardFill both blanks to create a computed property that filters an array.
iOS Swift
var filteredItems: [String] {
items.filter { $0.[1]([2]) }
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'starts(with)' with wrong substring or missing quotes.
✗ Incorrect
The filter method uses a closure that returns true for items containing the letter "a".
5fill in blank
hardFill all three blanks to filter a dictionary for positive values.
iOS Swift
let positiveScores = scores.filter { [1], [2] in [2] [3] 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering positive scores.
✗ Incorrect
This dictionary filter keeps players with scores greater than zero.