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

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.

主站蜘蛛池模板: 宾阳县| 木兰县| 南平市| 昂仁县| 定兴县| 仲巴县| 安阳县| 应城市| 桐乡市| 杭州市| 鸡西市| 灵台县| 蒙自县| 长治县| 额济纳旗| 清水河县| 龙门县| 黄大仙区| 安陆市| 两当县| 沙河市| 沛县| 泗洪县| 双牌县| 河源市| 龙岩市| 丽水市| 阜新| 昌乐县| 江门市| 萝北县| 梁河县| 丹棱县| 泉州市| 长白| 汶上县| 和硕县| 安溪县| 高碑店市| 武鸣县| 石渠县|