O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

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

Immutability

Swift makes it possible to define variables as mutable and immutable. The let keyword is used for immutable declarations and the var keyword is used for mutable declarations. Any variable that is declared with the let keyword will not be open to change. In the following examples, we define aMutableString with the var keyword so that we will be able to alter it later on; in contrast, we will not be able to alter aConstString that is defined with the let keyword:

var aMutableString = "This is a variable String" let aConstString = "This is a constant String" 

In FP, it is recommended to define properties as constants or immutables with let as much as possible. Immutable variables are easier to track and less error-prone. In some ...

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