You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Swift has a rich type system that balances safety with expressiveness. This lesson covers how to declare variables and constants, the built-in types, type inference, and type conversions.
In Swift, you declare constants with let and variables with var:
let maximumAttempts = 10 // constant — cannot be changed
var currentAttempt = 1 // variable — can be reassigned
currentAttempt = 2 // OK
// maximumAttempts = 20 // Error: cannot assign to a 'let' constant
Best Practice: Prefer
letovervarwhenever possible. The compiler will warn you if you usevarfor a value that is never mutated.
Swift uses type inference, but you can add explicit type annotations:
let name: String = "Alice"
let age: Int = 30
let pi: Double = 3.14159
let isActive: Bool = true
When the type can be inferred from the value, you can omit the annotation:
let name = "Alice" // inferred as String
let age = 30 // inferred as Int
let pi = 3.14159 // inferred as Double
let isActive = true // inferred as Bool
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.