Lesson Notes and Walkthrough
This lesson teaches the shape of a variable declaration in Swift. When you learn the pattern, writing code becomes faster and debugging becomes easier.
The Structure
var name: Type = valuevar: says this value can change.name: the label you use in code.Type: what kind of data (for exampleStringorInt).value: the starting data.
Beginner Example
var city: String = "Austin"
var temperature: Int = 72
print("\(city) is \(temperature) degrees.") Output: Austin is 72 degrees.
When Can You Skip the Type?
Swift can often infer the type automatically:
var city = "Austin" // inferred as String
var temperature = 72 // inferred as IntFor beginners, writing the type explicitly can make learning clearer.
Common Syntax Errors
- Using
:and=in the wrong places. - Mismatched types, like assigning text to an
Intvariable. - Forgetting quotation marks around strings.