这并不是关键问题,可以有解决方法,但仍然令人困惑。
请看下面的最简示例。我指的是已初始化的属性,在调用super.init()之前。为什么下面的语句会出现编译错误?使用表达式右侧的属性和左侧有什么特殊之处吗?
我查看了Swift语言指南,但没有找到任何相关信息。是Swift编译器出了问题,还是我在属性、self和init方面缺少了什么知识?或者在调用super.init之前,所有对"myProperty"的引用都应该出错?
(请注意,属性是常量(使用'let')还是其他类型,如Int,都无关紧要,情况都是一样的。)
class MyClass : NSObject {
var myProperty: Bool
override init() {
myProperty = true
if myProperty { /* this is ok */ }
if myProperty || true { /* this is ok */ }
if true || myProperty { /* this is NOT ok! ('self used before super.init') - WHY? */ }
super.init()
if true || myProperty { /* now this is ok */ }
}
}