I am trying to understand how lazy works in swift. The following code compiles without any error.
import UIKit
class Test : UIViewController{
let i = 1
lazy var j:Int = self.i
}
Whereas if I remove the type of j and make it inferred like the code below,
import UIKit
class Test : UIViewController{
let i = 1
lazy var j = self.i
}
I get compilation error "Value of type 'NSObject -> () -> Test' has no member 'i'"
Can someone explain what is going on with the compiler. Thanks
You need to consider 3 aspects here.
Let us consider following line from your non working code
lazy var j = self.i
Two problems are present in your non working code. First is you have not specified the data type of your lazy var. As the declaration is incomplete compiler won't treat 'j' as lazy var it will treat is as normal instance variable. You must be aware about swift's rule that whenever you declare any variable in the class you must assign some value to it or else you can make that variable optional. In above statement you are trying to assign i's value to j by using self within the class. When compiler is compiling that line no object of the class is created and the self you are using is nil that is why compiler is throwing the error(It will throw error even if you don't use self due to the same reason mentioned above).
Why it is not causing problems in your working code:
lazy var j:Int = self.i
As I have mentioned above the value to lazy var is assigned when you call that variable. How will you call that variable out side the class? Of course by creating object. And when you will call it with the help of object 'self' will point to the same object and self won't be nil it won't cause any problem.
In short two things you need to understand 3 things
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