objective-c - OC 在初始化方法或者是 dealloc 中最好不要使用属性,为什么?
習慣沉默
習慣沉默 2017-05-02 09:29:36
0
3
693

看过好几次这种描述:在初始化方法或者是 dealloc 中最好不要使用属性,因为无法确定 self 到底是不是确实调用的是你想要的实例,这个是为什么呢?

習慣沉默
習慣沉默

reply all(3)
漂亮男人

Where did you see it? Those are all things of the past, there are no restrictions in ARC, you can use it as you like.

In the old days of manual memory management, calling properties would involve adding and subtracting reference counters, and self = [super ...] 初始化的对象不一定是当前类的对象,可能是“变体”或私有类(objc里的class有 classmetaclass 的区别),所以在你不知道属性 setter 方法里到底是怎么加减引用计数器的情况下,你就只针对 ivar just retain and release.

- (id)initWithString:(NSString *)str
{
    self = [super init];
    if (self) {
        _foo = [str copy];
    }
    return self;
}

- (void)dealloc
{
    [_foo release];
    [super dealloc];
}

retain(copy,new) and release appear in pairs

给我你的怀抱

Because it will generate circular references, such as delegate, and then it will never be released

漂亮男人

Because there are categories (Category), setters, getters, KVO, etc. that need to be paid attention to
The most important thing is because of the classification. Grammar can call the attributes and methods of the classification, but _ does not work, you need to pay attention

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