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

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.

主站蜘蛛池模板: 绿春县| 云林县| 广汉市| 阜新市| 靖西县| 盐津县| 邻水| 英德市| 昭通市| 武宁县| 桐庐县| 定西市| 楚雄市| 河南省| 襄垣县| 独山县| 靖西县| 张北县| 凤山县| 永和县| 页游| 乐安县| 连云港市| 镇巴县| 南投县| 台湾省| 太仓市| 四会市| 博野县| 昌平区| 惠安县| 东山县| 临朐县| 石渠县| 绿春县| 阳东县| 闽侯县| 景东| 通化市| 克山县| 台南县|