0
0
iOS Swiftmobile~20 mins

Why testing ensures app quality in iOS Swift - Build It to Prove It

Choose your learning style9 modes available
Build: Simple Counter with Test
This screen shows a number that can be increased by tapping a button. It includes a test to check if the counter works correctly. Testing helps catch mistakes early and keeps the app working well.
Target UI
-------------------
|   Simple Counter  |
|------------------|
|                  |
|       0          |
|                  |
|  [+] Increase     |
-------------------
Display a number starting at 0
Add a button labeled '+' that increases the number by 1 when tapped
Write a unit test to verify the number increases correctly
Show how testing helps ensure the app works as expected
Starter Code
iOS Swift
import SwiftUI

struct CounterView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 40) {
            Text("Simple Counter")
                .font(.title)
                .padding()
            Text("\(count)")
                .font(.system(size: 72, weight: .bold))
            Button(action: {
                // TODO: Increase count by 1
            }) {
                Text("+")
                    .font(.largeTitle)
                    .frame(width: 80, height: 80)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .clipShape(Circle())
            }
        }
    }
}

struct CounterView_Previews: PreviewProvider {
    static var previews: some View {
        CounterView()
    }
}

// TODO: Add unit test for CounterView logic
Task 1
Task 2
Solution
iOS Swift
import SwiftUI

struct CounterView: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 40) {
            Text("Simple Counter")
                .font(.title)
                .padding()
            Text("\(count)")
                .font(.system(size: 72, weight: .bold))
            Button(action: {
                count += 1
            }) {
                Text("+")
                    .font(.largeTitle)
                    .frame(width: 80, height: 80)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .clipShape(Circle())
                    .accessibilityLabel("Increase count")
            }
        }
    }
}

// Unit test example
import XCTest

final class CounterViewTests: XCTestCase {
    func testIncrement() {
        var count = 0
        // Simulate button tap increasing count
        count += 1
        XCTAssertEqual(count, 1, "Count should increase by 1")
    }
}

struct CounterView_Previews: PreviewProvider {
    static var previews: some View {
        CounterView()
    }
}

This app shows a number starting at zero. When the user taps the '+' button, the number increases by one. This is done by adding count += 1 inside the button's action.

The unit test simulates increasing the count and checks if the value is correct. Testing like this helps catch errors early, so the app behaves as expected and stays reliable.

Accessibility label is added to the button for screen readers, making the app friendly for all users.

Final Result
Completed Screen
-------------------
|   Simple Counter  |
|------------------|
|                  |
|       1          |
|                  |
|  [+] Increase     |
-------------------
User taps the '+' button
The number on screen increases by 1 each tap
Unit test confirms the increment logic works correctly
Stretch Goal
Add a reset button to set the counter back to zero
💡 Hint
Add another button labeled 'Reset' that sets count = 0 when tapped