Complete the code to declare the main app struct in SwiftUI.
import SwiftUI @main struct MyApp: [1] { var body: some Scene { WindowGroup { ContentView() } } }
The main app struct must conform to the App protocol to launch the SwiftUI app.
Complete the code to create a SwiftUI view named ContentView.
import SwiftUI struct ContentView: [1] { var body: some View { Text("Hello, world!") } }
A SwiftUI view must conform to the View protocol to display UI elements.
Fix the error in the code by completing the missing property wrapper for state management.
struct CounterView: View {
[1] var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increment") {
count += 1
}
}
}
}The @State property wrapper allows the view to update when the variable changes.
Fill both blanks to create a filter-map chain that filters words longer than 3 characters.
let words = ["apple", "cat", "dog", "banana"] let filtered = words.filter { $0.[1] > 3 }.map { $0.[2] }
Use count to get the length of the word for filtering, and uppercased() to convert the word to uppercase.
Fill all three blanks to create a SwiftUI view with a button that toggles a boolean state.
struct ToggleView: View {
[1] var isOn = false
var body: some View {
Button(action: {
isOn = !isOn
}) {
Text(isOn ? [2] : [3])
}
}
}@State manages the boolean state. The button text shows "On" when true and "Off" when false.