class Shape { var numberOfSIDes = 0 var name: String init(name:String) { self.name = name } func simpleDescription() -> String { return "A shape with \(numberOfSIDes) sIDes." }}class Square: Shape { var sIDeLength: Double init(sIDeLength:Double,name:String) { super.init(name:name) // Error here self.sIDeLength = sIDeLength numberOfSIDes = 4 } func area () -> Double { return sIDeLength * sIDeLength }}
使用上面的实现我得到错误:
property 'self.sIDeLength' not initialized at super.init call super.init(name:name)
为什么我必须在调用super.init之前设置self.sIDeLength?
报价从Swift编程语言,它回答你的问题:总结“Swift’s compiler performs four helpful safety-checks to make sure
that two-phase initialization is completed without error:”Safety check 1 “A designated initializer must ensure that all of the
“propertIEs introduced by its class are initialized before it
delegates up to a superclass initializer.”Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. 07000
以上是内存溢出为你收集整理的属性 – Swift类中的错误:属性未在super.init调用初始化全部内容,希望文章能够帮你解决属性 – Swift类中的错误:属性未在super.init调用初始化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)