Bird
Raised Fist0
iOS Swiftmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Swift data type is used to store true or false values?
easy
A. Int
B. Double
C. String
D. Bool

Solution

  1. Step 1: Understand the data types

    Bool stores true or false values, Int stores whole numbers, String stores text, and Double stores decimal numbers.
  2. Step 2: Match the description to the type

    Since the question asks for true or false values, the correct type is Bool.
  3. Final Answer:

    Bool -> Option D
  4. Quick Check:

    Bool = true/false [OK]
Hint: True/false values always use Bool type [OK]
Common Mistakes:
  • Confusing Bool with Int
  • Using String for true/false
  • Choosing Double for boolean values
2. Which of the following is the correct way to declare a Double variable named price with value 9.99 in Swift?
easy
A. var price: Double = 9.99
B. var price = "9.99"
C. var price Double = 9.99
D. var price: Int = 9.99

Solution

  1. Step 1: Check variable declaration syntax

    In Swift, to declare a variable with a specific type, use var name: Type = value.
  2. Step 2: Identify the correct type and value

    Since 9.99 is a decimal number, the type must be Double. var price: Double = 9.99 correctly declares price as Double with 9.99.
  3. Final Answer:

    var price: Double = 9.99 -> Option A
  4. Quick Check:

    Double declaration uses var name: Double = value [OK]
Hint: Use var name: Double = value for decimals [OK]
Common Mistakes:
  • Assigning decimal to Int
  • Using quotes for numbers
  • Omitting type when needed
3. What will be the output of this Swift code?
let number: Int = 5
let text: String = "5"
let isEqual = (number == Int(text)!)
print(isEqual)
medium
A. true
B. false
C. 5
D. Compilation error

Solution

  1. Step 1: Understand the code logic

    number is an Int with value 5, text is a String "5". Int(text) converts the string "5" to integer 5.
  2. Step 2: Compare values

    number == Int(text) compares 5 == 5, which is true. So isEqual is true and print outputs true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    5 == 5 is true [OK]
Hint: Convert String to Int before comparing [OK]
Common Mistakes:
  • Comparing Int and String directly
  • Forgetting Int(text) conversion
  • Expecting string output
4. Identify the error in this Swift code snippet:
var isActive: Bool = "true"
print(isActive)
medium
A. print() cannot print Bool values
B. Bool cannot be assigned a String value
C. Variable name is invalid
D. Missing semicolon at end of line

Solution

  1. Step 1: Analyze variable assignment

    isActive is declared as Bool but assigned the string "true" (in quotes), which is incorrect.
  2. Step 2: Understand type mismatch

    Bool must be assigned true or false without quotes. Assigning a String causes a type error.
  3. Final Answer:

    Bool cannot be assigned a String value -> Option B
  4. Quick Check:

    Bool = true/false only, no quotes [OK]
Hint: Bool values are true or false without quotes [OK]
Common Mistakes:
  • Using quotes around true/false
  • Expecting semicolon error
  • Thinking print can't show Bool
5. You want to store a user's age, height in meters, full name, and whether they are a student. Which Swift data types should you use for these variables respectively?
hard
A. String, String, Int, Bool
B. Double, Int, Bool, String
C. Int, Double, String, Bool
D. Bool, Int, Double, String

Solution

  1. Step 1: Match each data to its type

    Age is a whole number -> Int, height with decimals -> Double, full name is text -> String, student status true/false -> Bool.
  2. Step 2: Verify option matches order

    Int, Double, String, Bool lists Int, Double, String, Bool in that order, matching the variables correctly.
  3. Final Answer:

    Int, Double, String, Bool -> Option C
  4. Quick Check:

    Whole number, decimal, text, true/false [OK]
Hint: Match data meaning: whole, decimal, text, true/false [OK]
Common Mistakes:
  • Mixing Int and Double
  • Using Bool for text
  • Confusing order of types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard