Data types help us tell the app what kind of information we want to store, like numbers, words, or true/false answers.
Data types (Int, Double, String, Bool) in 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.
var age: Int = 25var temperature: Double = 36.6var greeting: String = "Hello!"var isLoggedIn: Bool = false
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.
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() } } }
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.
Int stores whole numbers.
Double stores decimal numbers.
String stores text inside quotes.
Bool stores true or false values.
Practice
Solution
Step 1: Understand the data types
Bool stores true or false values, Int stores whole numbers, String stores text, and Double stores decimal numbers.Step 2: Match the description to the type
Since the question asks for true or false values, the correct type is Bool.Final Answer:
Bool -> Option DQuick Check:
Bool = true/false [OK]
- Confusing Bool with Int
- Using String for true/false
- Choosing Double for boolean values
Solution
Step 1: Check variable declaration syntax
In Swift, to declare a variable with a specific type, use var name: Type = value.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.Final Answer:
var price: Double = 9.99 -> Option AQuick Check:
Double declaration uses var name: Double = value [OK]
- Assigning decimal to Int
- Using quotes for numbers
- Omitting type when needed
let number: Int = 5 let text: String = "5" let isEqual = (number == Int(text)!) print(isEqual)
Solution
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.Step 2: Compare values
number == Int(text) compares 5 == 5, which is true. So isEqual is true and print outputs true.Final Answer:
true -> Option AQuick Check:
5 == 5 is true [OK]
- Comparing Int and String directly
- Forgetting Int(text) conversion
- Expecting string output
var isActive: Bool = "true" print(isActive)
Solution
Step 1: Analyze variable assignment
isActive is declared as Bool but assigned the string "true" (in quotes), which is incorrect.Step 2: Understand type mismatch
Bool must be assigned true or false without quotes. Assigning a String causes a type error.Final Answer:
Bool cannot be assigned a String value -> Option BQuick Check:
Bool = true/false only, no quotes [OK]
- Using quotes around true/false
- Expecting semicolon error
- Thinking print can't show Bool
Solution
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.Step 2: Verify option matches order
Int, Double, String, Bool lists Int, Double, String, Bool in that order, matching the variables correctly.Final Answer:
Int, Double, String, Bool -> Option CQuick Check:
Whole number, decimal, text, true/false [OK]
- Mixing Int and Double
- Using Bool for text
- Confusing order of types
Ready to prove it?
Pass the test and this pattern moves unknown
