0
0
iOS Swiftmobile~20 mins

Data types (Int, Double, String, Bool) in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Data Types Demo
This screen shows examples of four basic data types in Swift: Int, Double, String, and Bool. It displays each value with a label so users can see how different data types look in a simple app.
Target UI
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value:            |
| Double value:         |
| String value:         |
| Bool value:           |
|                       |
| [Refresh Values]      |
-------------------------
Display four labels showing values for Int, Double, String, and Bool variables.
Add a button labeled 'Refresh Values' that changes the values to new random or sample data when tapped.
Use Swift data types correctly and show their values as text.
Layout the labels vertically with some spacing.
Make sure the button is below the labels and centered.
Starter Code
iOS Swift
import SwiftUI

struct DataTypesDemoView: View {
    // TODO: Add state variables for Int, Double, String, Bool

    var body: some View {
        VStack(spacing: 20) {
            // TODO: Show labels for each data type value

            Button("Refresh Values") {
                // TODO: Update values when button tapped
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .padding()
    }
}

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

struct DataTypesDemoView: View {
    @State private var intValue: Int = 10
    @State private var doubleValue: Double = 3.14
    @State private var stringValue: String = "Hello"
    @State private var boolValue: Bool = true

    var body: some View {
        VStack(spacing: 20) {
            Text("Int value: \(intValue)")
            Text("Double value: \(doubleValue)")
            Text("String value: \(stringValue)")
            Text("Bool value: \(boolValue ? "True" : "False")")

            Button("Refresh Values") {
                intValue = Int.random(in: 1...100)
                doubleValue = Double.random(in: 0...10).rounded(toPlaces: 2)
                stringValue = ["Hello", "Swift", "World", "Data"].randomElement() ?? "Swift"
                boolValue.toggle()
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .padding()
    }
}

extension Double {
    func rounded(toPlaces places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}

struct DataTypesDemoView_Previews: PreviewProvider {
    static var previews: some View {
        DataTypesDemoView()
    }
}

This app uses @State variables to hold values of four basic Swift data types: Int, Double, String, and Bool. Each value is shown in a Text view with a label.

The "Refresh Values" button changes these values to new random or sample data. For example, the Int changes to a random number between 1 and 100, the Double changes to a random decimal rounded to 2 places, the String picks a random word from an array, and the Bool toggles between true and false.

This shows how to declare and use basic data types in SwiftUI and update the UI reactively when values change.

Final Result
Completed Screen
-------------------------
| Data Types Demo       |
|-----------------------|
| Int value: 42         |
| Double value: 7.28    |
| String value: Swift   |
| Bool value: False     |
|                       |
| [Refresh Values]      |
-------------------------
When user taps 'Refresh Values', all four values update to new random or sample data.
The Bool value switches between True and False each tap.
Stretch Goal
Add a toggle switch to switch between light and dark mode for the screen.
💡 Hint
Use SwiftUI's @Environment(\.colorScheme) and .preferredColorScheme modifier to change appearance.