本文介绍了iVar属性,通过自我访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当访问属性的setter / getter方法时,我应该使用[ self setThisValue:@a]; ,而不是 thisValue = @a; 然而在下面的例子中我可以看到添加自我文档,我发送消息到iVar属性,而不是局部范围的变量,但它做任何其他的

I understand that when accessing setter / getter methods for properties I should be using [self setThisValue:@"a"]; rather than thisValue = @"a"; However with the example below I can see that adding self documents that I am sending a message to an iVar property rather than a locally scoped variable, but does it do anything else in this case?

@interface CustomController : UIViewController {
    NSMutableArray *foundList;
}
@property(nonatomic, retain) NSMutableArray *foundList;
@end

[[self foundList] addObject:eachObject]; // I usually write this ...

[foundList addObject:eachObject];

gary。

推荐答案

如果你有一个ivar的定义属性,你应该使用它,而不是直接访问ivar。这允许子类重写setter / getter并做一些不同的只是从ivar获取值。

If you have a defined property for an ivar, you should use it rather than accessing the ivar directly. That allows subclasses to override the setter/getter and do something different to just fetching the value from the ivar.

唯一的例外是init方法和dealloc。

The only exception is in init methods and dealloc.

这篇关于iVar属性,通过自我访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:54