Swift Data Types
Swift Data Types
Swift has built-in types like Int, Double, Bool and String.
Swift uses type inference to deduce the type from the value.
Basic Types
Declare constants with let and variables with var.
Swift infers the type from the initial value.
Example
let anInt = 42 // Int
let aDouble = 3.14 // Double
let isSwiftFun = true // Bool
let greeting = "Hello" // String
print(aDouble)
print(isSwiftFun)
print(greeting)
This example declares common Swift types and prints them.
Type Inference vs Annotation
Swift usually infers the type from the initial value, but you can add a type for clarity.
Example
let inferred = 10 // Int (inferred)
let annotated: Double = 3 // Double (explicit)
print(type(of: inferred), type(of: annotated))
This example contrasts inferred vs. explicit types.