今天碰到一個問題,就是用到懶加載的時候,我用了self,結(jié)果報錯直接蹦了,
我們老師說這是self遞歸引用了,可我還是不明白它們之間的區(qū)別
人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!
Self. _x is the automatically created instance variable.
For example, you define the following attribute:
@property(nonation, strong) NSString *x;
There is a bunch of hidden (simplified) code as follows:
NSString *_x;
-(NSString *)x {
return _x;
}
-(void)setX:(NSString *)x {
_x = x;
}
I guess your lazy loading code overloads the get method of the attribute. Self.x actually calls the [self x] method. If you use self.x in the get method, then self.x calls it again. , [self x] method, this is infinite recursion.
If it is referenced, there will be no difference. It is the same pointer. If it is assigned, there is a difference. self.xx=oo First, xxretaincount -1 and then retain oo _XX is copied to point directly to oo. There is no retain step. Nor
I just guessed that you might be:
self.some = [self some];
-(type)some{
self.some = [...];
}
Then when you call self.some, it is equivalent to using [self some], and self.some in some calls [self some] again. . . A loop is formed. . .
Underscore means direct access, bypassing set and get. .