objective-c - ios开发中self和下划线的区分
黄舟
黄舟 2017-04-27 09:02:53
0
3
868

今天碰到一个问题,就是用到懒加载的时候,我用了self,结果报错直接蹦了,

我们老师说这是self递归引用了,可我还是不明白它们之间的区别

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
洪涛

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. .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template