官术网_书友最值得收藏!

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 cases, such as CoreData programming, the software development kit (SDK) requires mutable properties; however, in these cases, it is recommended to use mutable variables.

Immutability and stateless programming will be covered in detail in Chapter 9, Importance of Immutability.

主站蜘蛛池模板: 南投县| 镇康县| 山西省| 东海县| 大荔县| 奉新县| 慈溪市| 普宁市| 宝鸡市| 元朗区| 大方县| 华阴市| 水富县| 定安县| 大丰市| 南陵县| 武安市| 海晏县| 沙洋县| 台前县| 托克逊县| 宜宾市| 永吉县| 扎兰屯市| 阿勒泰市| 蒙阴县| 鄂温| 新郑市| 莆田市| 饶河县| 云安县| 噶尔县| 尖扎县| 永寿县| 伊宁市| 锦州市| 平阳县| 庄河市| 开化县| 邵东县| 楚雄市|