Button syntax uses closures for action and label [OK]
Hint: Button uses closures: action first, label second [OK]
Common Mistakes:
Using Button with string only as label
Putting action inside label
Incorrect parameter order
3. What will be displayed when this SwiftUI code runs?
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increment") {
count += 1
}
}
}
}
medium
A. A text showing "Count: 0" and a button labeled "Increment" that increases count when tapped.
B. Only a button labeled "Increment" with no text displayed.
C. A text showing "Count: 1" initially and a button labeled "Increment".
D. The app will crash due to missing @State annotation.
Solution
Step 1: Understand @State usage
The variable count is marked with @State, so changes update the UI.
Step 2: Analyze initial UI
Initially, count is 0, so text shows "Count: 0" and button labeled "Increment" is visible.
Final Answer:
A text showing "Count: 0" and a button labeled "Increment" that increases count when tapped. -> Option A
Quick Check:
@State variable updates UI on change [OK]
Hint: @State variables start at initial value and update UI [OK]
Common Mistakes:
Thinking count starts at 1
Assuming no text is shown
Believing app crashes without @State
4. Identify the error in this SwiftUI code snippet:
struct ContentView: View {
var body: some View {
Button("Click me") {
print("Clicked")
}
}
}
medium
A. The body property must return some View explicitly.
B. No syntax error; the code is correct.
C. The body property must be marked with @ViewBuilder.
D. The Button label should be a closure, not a string.
Solution
Step 1: Check body property syntax
In SwiftUI, the body computed property implicitly returns the view using @ViewBuilder.
Step 2: Verify the Button syntax
Button("Click me") { print("Clicked") } is valid; no explicit return needed even for multi-line expressions.
Final Answer:
No syntax error; the code is correct. -> Option B
Quick Check:
SwiftUI body implicitly returns views [OK]
Hint: SwiftUI body uses implicit return via ViewBuilder [OK]
Common Mistakes:
Thinking an explicit return is required
Believing @ViewBuilder must be marked explicitly
Assuming Button requires closure label only
5. You want to create a simple iOS app that shows "Hello, SwiftUI!" and a button that changes the text to "Button tapped!" when pressed. Which code snippet correctly implements this behavior?
hard
A. struct ContentView: View {
@State private var message = "Hello, SwiftUI!"
var body: some View {
VStack {
Text("Hello, SwiftUI!")
Button("Tap") {
message = "Button tapped!"
}
}
}
}
B. struct ContentView: View {
var message = "Hello, SwiftUI!"
var body: some View {
VStack {
Text(message)
Button("Tap") {
message = "Button tapped!"
}
}
}
}
C. struct ContentView: View {
var message = "Hello, SwiftUI!"
var body: some View {
VStack {
Text(message)
Button { message = "Button tapped!" } label: { Text("Tap") }
}
}
}
D. struct ContentView: View {
@State private var message = "Hello, SwiftUI!"
var body: some View {
VStack {
Text(message)
Button("Tap") {
message = "Button tapped!"
}
}
}
}
Solution
Step 1: Use @State for mutable text
The text changes when the button is tapped, so message must be a @State variable.
Step 2: Update text on button tap
The button action updates message to "Button tapped!" and the UI reflects this change.
Step 3: Verify correct syntax
struct ContentView: View {
@State private var message = "Hello, SwiftUI!"
var body: some View {
VStack {
Text(message)
Button("Tap") {
message = "Button tapped!"
}
}
}
} uses @State private var, a VStack with Text(message), and a button with label and action correctly.
Final Answer:
A -> Option D
Quick Check:
@State variable updates text on button tap [OK]
Hint: Use @State for text that changes on button tap [OK]
Common Mistakes:
Not using @State for mutable text
Using constant text in Text view
Incorrect button label or action syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.