问题
Y this giving this error - Cannot use instance member 'getA' within property initializer; property initializers run before 'self' is available
class A {
var asd : String = getA()
func getA() -> String {
return "A"
}
}
回答1:
Property initializer run before self is available.
The solution is to lazy initialize the property:
class A {
lazy var asd: String = getA()
func getA() -> String {
return "A"
}
}
That will initialize the property first time you are trying to use it.
回答2:
You first need to initialize your asd variable. Then in the init you can apply your function value to it.
class A {
var asd : String = ""
init() {
self.asd = self.getA()
}
func getA() -> String {
return "A"
} }
来源:https://stackoverflow.com/questions/53724345/cannot-use-instance-member-geta-within-property-initializer-property-initiali