0
0
iOS Swiftmobile~5 mins

Data types (Int, Double, String, Bool) in iOS Swift

Choose your learning style9 modes available
Introduction

Data types help us tell the app what kind of information we want to store, like numbers, words, or true/false answers.

When you want to store a whole number like age or count.
When you need to store decimal numbers like price or temperature.
When you want to store text like a name or message.
When you want to store a true or false value like if a switch is on or off.
Syntax
iOS Swift
var number: Int = 10
var price: Double = 9.99
var name: String = "Alice"
var isOn: Bool = true

Int is for whole numbers without decimals.

Double is for numbers with decimals.

String is for text inside double quotes.

Bool is for true or false values.

Examples
This stores the whole number 25 as an integer.
iOS Swift
var age: Int = 25
This stores a decimal number for temperature.
iOS Swift
var temperature: Double = 36.6
This stores text inside quotes.
iOS Swift
var greeting: String = "Hello!"
This stores a false value for a login status.
iOS Swift
var isLoggedIn: Bool = false
Sample App

This app shows four pieces of information using different data types. It displays age as a whole number, price with decimals, a name as text, and a true/false status as Yes or No.

iOS Swift
import SwiftUI

struct ContentView: View {
    let age: Int = 30
    let price: Double = 19.99
    let name: String = "Bob"
    let isOn: Bool = true

    var body: some View {
        VStack(spacing: 20) {
            Text("Age: \(age)")
            Text("Price: $\(price)")
            Text("Name: \(name)")
            Text("Is On: \(isOn ? "Yes" : "No")")
        }
        .font(.title)
        .padding()
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
OutputSuccess
Important Notes

You can change the value of variables if you use var, but constants with let cannot be changed.

Use the correct data type to avoid errors and make your app work smoothly.

Strings must always be inside double quotes.

Summary

Int stores whole numbers.

Double stores decimal numbers.

String stores text inside quotes.

Bool stores true or false values.