Variable 'xxx' was never mutated, consider changing to 'let'

来源:互联网 发布:剑雨逍遥手游进阶数据 编辑:程序博客网 时间:2024/06/17 02:00

var emptyArray = [String]()
var emptyDictionary = [String :String]()

print("the empty array is \n ", emptyArray, emptyDictionary)

解释如下:

They talked about this in the WWDC videos and the release notes.

It has always been the case that you get much, much better performance (faster speed, smaller space) if you uselet instead of var whenever you can. This tells the compiler that this thing is aconstant, not a variable, and that fact allows the compiler to optimize all kinds of things away.

But the compiler can't do that unless you do use let whenever you can. It won't change avar to a let for you.

Therefore, in Swift 2, the compiler does a smarter analysis at build time and warns you if you are usingvar where you could have used let. Eventually this feature will work properly, at which point you should take the compiler's advice!


如果变量本事就是不可变的

那么尽量从var改为let

这样Swift的编译器可以对let进行优化,从而获得更好的性能

再去按照要求去改为:

let emptyArray = [String]()
let emptyDictionary = [String :String]()

print("the empty array is \n ", emptyArray, emptyDictionary)

打印结果为:the empty array is [] [:]

0 0
原创粉丝点击