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

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.

主站蜘蛛池模板: 阳城县| 于都县| 昌江| 潜江市| 松滋市| 云和县| 杭锦后旗| 岱山县| 南和县| 阿图什市| 布拖县| 南平市| 宝坻区| 象州县| 湖南省| 天长市| 六盘水市| 同心县| 灵璧县| 保定市| 婺源县| 固始县| 阿克苏市| 新余市| 荥阳市| 诸城市| 怀仁县| 霍州市| 中超| 保靖县| 金门县| 安陆市| 兴业县| 广安市| 鹤山市| 兰西县| 克拉玛依市| 遂溪县| 井陉县| 麻城市| 阿克|