O'Reilly logo

Learn Swift on the Mac: For OS X and iOS by Waqar Malik

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    5

Constants, Variables, and Data Types

In OOP languages, an immutable object is an object whose state can’t be modified. Swift calls these constants. Once a constant object has been created and an initial value assigned, it can’t be changed. In contrast, an object whose value can be changed is a mutable object. Swift calls these variables.

In Swift, variables must be defined and have a valid value before they can be used. Use the var keyword to define a variable and the let keyword to define a constant. Note that all types can be defined either as mutable or immutable (constant or variable) based on whether they’re defined with let or var. The language doesn’t require separate mutable and immutable types:

let numberOfWheelsForBicycle ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required