正如Apple文档所说:
Use
let
to make a constant andvar
to make a variable. The value of a constant doesn’t need to be known at compile time,but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.
让我们考虑一下课程:
class A { var a = [String]() }
由于数组a是可变的,因此它是通过var定义的.但是如果我们考虑B类,A的实例是属性呢?
class B { let b = A() }
即使b是可变的,让关键字也可以,因为引用不会被改变.另一方面,var也可以,因为b的内容可以改变.我应该在这个例子中选择什么 – let或var?