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"
}
}
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.
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"
} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With