class Foo {
let fooValue = 1
}
print(Foo.fooValue) // not work
class Bar {
static let barValue = 1
}
print(Bar.barValue) // work; print "1"
为什么?我期望Foo
的例子能够正常工作,因为fooValue
的值是不变的,值和编译时间中已知的内存地址。但我需要使用关键字static
才能工作。“let”和“static let”之间的区别是什么?
作为一个'let'只是意味着它是一个常数 - 它与该属性是无关的可以从实例或静态作用域访问。另请参阅[如果在“swift”中使用“let”关键字来定义常量/不可变项,有什么用“static”关键字](http://stackoverflow.com/q/34574876/2976878) – Hamish