什么时候自己访问属性,什么时候不去? [英] When to access property with self and when not to?

查看:129
本文介绍了什么时候自己访问属性,什么时候不去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释设置 someObject = someOtherObject; self.someObject = someOtherObject; 之间的区别,如果someObject是使用@property创建的类属性(非原子,保留)SomeType someObject;

Can anyone explain the difference between setting someObject = someOtherObject; and self.someObject = someOtherObject; if someObject is a class property created with @property (nonatomic, retain) SomeType someObject;

为了澄清我有类似的东西:

To clarify I have something like:

@interface SomeClass : NSObject {
   SomeType* someObject;
}

@property (nonatomic, retain) SomeType* someObject;

@end

我注意到有时候我使用EXC_BAD ACCESS没有自我的财产,似乎很随意。当我使用self时,我的程序应该是应有的。当我跳过self时,我没有得到任何编译器错误或警告,所以我猜它是某种有效的语法?

I have noticed I get EXC_BAD ACCESS sometimes when I use the property without self and it seems quite random. When I use self my program acts as it should be. I don’t get any compiler errors or warnings when I skip self so I guess it is somehow valid syntax?

推荐答案

属性是只是访问数据的便捷方式。所以当你声明属性@property(非原子,保留)SomeType * someObject;这意味着在访问期间将合成2种方法:

Properties are just a convenient way to access the data. So when you are declaring the property @property (nonatomic, retain) SomeType* someObject; this means that during access there would be synthesized 2 methods:

getter:

-(SomeType*) someObject {
   return someObject;
}

setter

-(void) setSomeObject:(SomeType*) obj {
   [someObject release];
   someObject = [obj retain];
}

因此属性和ivars之间的主要区别在于动态创建setter的属性/ getter方法(你可以覆盖它们)。但是当你编写someObject = new_val时,你只是将引用复制到内存位置。除了一个汇编指令外,没有其他工作要做。

So the main difference between properties and ivars is that properties dynamically creating the setter/getter methods (and you can override them). But when you're writing someObject = new_val, you're just copying the reference to the memory location. No additional work is done there except one assembly instruction.

还有一件事需要提及:原子和非原子。
使用原子,合成的setter / getter将确保始终从getter返回整个值或由setter设置,而不管任何其他线程上的setter活动。也就是说,如果线程A位于getter的中间,而线程B调用setter,则实际可行的值 - 一个自动释放的对象,很可能 - 将返回给A中的调用者。

There is one more thing to mention: atomic and nonatomic. With atomic, the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A.

在非原子,没有这样的保证。因此,非原子比原子快得多。

In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than atomic.

编辑:所以如果你有一些变量,可以从不同的线程访问或/和一些额外的工作必须完成(例如,保留,举起一些旗帜......),然后你选择的是财产。但有时你有一个变量,经常被访问,并且通过属性访问会导致很大的开销,因为处理器必须执行更多操作来合成和调用方法。

so if you have some variable, that is accessed from different threads or/and some additional work has to be done (e.g. retain, raise some flags ...), then your choice is property. But sometimes you have a variable, that is accessed very often and access via property can lead to a big overhead, because processor has to perform much more operations to synthesize and call method.

这篇关于什么时候自己访问属性,什么时候不去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆