0
0
iOS Swiftmobile~20 mins

Why Swift is designed for safety and speed in iOS Swift - Build It to Prove It

Choose your learning style9 modes available
Build: Swift Safety and Speed
This screen explains why Swift is designed to be safe and fast with simple examples.
Target UI
-----------------------------
| Swift Safety and Speed    |
|---------------------------|
| Why Swift?                |
| - Safe code               |
| - Fast performance        |
|                           |
| Example:                  |
| let x: Int = 5            |
| let y = x + 10            |
|                           |
| [Show Explanation]        |
-----------------------------
Display a title at the top
Show two bullet points explaining safety and speed
Show a simple Swift code example
Add a button labeled 'Show Explanation'
When button is tapped, show a short explanation text below the button
Starter Code
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var showExplanation = false

    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("Swift Safety and Speed")
                .font(.title)
                .bold()

            Text("Why Swift?")
                .font(.headline)

            // TODO: Add bullet points here

            // TODO: Add code example here

            Button(action: {
                showExplanation.toggle()
            }) {
                Text("Show Explanation")
            }

            // TODO: Show explanation text when button is tapped

            Spacer()
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Task 1
Task 2
Task 3
Solution
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var showExplanation = false

    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("Swift Safety and Speed")
                .font(.title)
                .bold()

            Text("Why Swift?")
                .font(.headline)

            VStack(alignment: .leading, spacing: 8) {
                Text("• Safe code: Swift helps avoid common errors like null pointers.")
                Text("• Fast performance: Swift is optimized to run quickly on devices.")
            }

            Text("Example:")
                .font(.headline)

            Text("let x: Int = 5\nlet y = x + 10")
                .font(.system(.body, design: .monospaced))
                .padding(8)
                .background(Color.gray.opacity(0.1))
                .cornerRadius(5)

            Button(action: {
                showExplanation.toggle()
            }) {
                Text(showExplanation ? "Hide Explanation" : "Show Explanation")
                    .foregroundColor(.blue)
            }

            if showExplanation {
                Text("Swift is designed to catch mistakes early and run your code fast, making apps safer and smoother.")
                    .font(.body)
                    .padding(.top, 8)
            }

            Spacer()
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This screen uses a vertical stack to organize the content clearly.

We show a title and a subtitle, then two bullet points explaining safety and speed in simple words.

The Swift code example uses a monospaced font and a light background to look like code.

The button toggles showing or hiding the explanation text below it.

This approach keeps the UI simple and friendly, helping beginners understand why Swift is safe and fast.

Final Result
Completed Screen
-----------------------------
| Swift Safety and Speed    |
|---------------------------|
| Why Swift?                |
| • Safe code: Swift helps  |
|   avoid common errors.    |
| • Fast performance: Swift |
|   runs quickly on devices.|
|                           |
| Example:                  |
| let x: Int = 5            |
| let y = x + 10            |
|                           |
| [Show Explanation]        |
|                           |
| Swift is designed to catch|
| mistakes early and run    |
| your code fast, making    |
| apps safer and smoother.  |
-----------------------------
Tapping 'Show Explanation' reveals the explanation text below the button.
Tapping 'Hide Explanation' hides the explanation text again.
Stretch Goal
Add a toggle to switch between light and dark mode for the screen.
💡 Hint
Use SwiftUI's @Environment(\.colorScheme) and a toggle button to switch modes.