本文介绍了为了拥有ARC的强大性能,是否需要self.iVar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如果我声明一个强属性,就像这样:

  1. If I declare a property strong, like so:

@property (strong, nonatomic) UIView *iVar;

设置时,是否执行iVar = ...self.iVar = ...无关紧要?似乎使用ARC,他们可以做同样的事情.

When I'm setting it, does it matter if I do iVar = ... or self.iVar = ...? It seems that with ARC, they do the same thing.

如果我仅声明实例变量(而不是@property),例如BOOL selected,是否表示它被推断为__unsafe_unretained(因为没有属性将其指定为强),或者必须我明确指定了吗?

If I only declare the instance variable (not the @property), e.g., BOOL selected, does that mean it's inferred to be __unsafe_unretained (since there's no property specifying it to be strong), or must I explicitly specify that?

似乎我在回答 ARC:如何释放静态变量?,但是我对上述问题仍然感到困惑.

It seems like I may have answered my own questions above in answering ARC: How to release static variable?, but I'm still slightly confused on the above questions.

推荐答案

从内存管理的角度,使用ivar = ...self.property = ...(注意:没有 这样的东西self.ivar)是相同的.但是,使用ivar = ...不会调用setter,而使用self.property = ...则会调用setter.这有3个重要的分支,没有特定的顺序:

From a memory management perspective, using ivar = ... or self.property = ... (note: there's no such thing as self.ivar) are the same. However, using ivar = ... doesn't invoke the setter while self.property = ... does. This has 3 important ramifications, in no particular order:

  1. 如果该属性未标记为nonatomic,则对基础ivar的访问将不会获得锁定,并且您将破坏原子性的含义.
  2. 如果您或子类重写了该属性,则不会调用该重写的setter.
  3. 将不会发送KVO通知.
  1. If the property is not marked nonatomic, then access to the underlying ivar will not take the lock and you will be breaking the atomicity implications.
  2. If the property is overridden, either by you or by a subclass, the overridden setter will not be invoked.
  3. KVO notifications will not be sent.

至于仅声明ivar,它具有与声明局部变量相同的内存管理语义. Objective-C自动参考计数文档的第4.4节中对此进行了记录,但基本上,如果它是一个对象,将被推断为__strong.

As for only declaring the ivar, it has the same memory management semantics as declaring a local variable. This is documented in section 4.4 of the Objective-C Automatic Reference Counting document, but basically, if it's an object, it will be inferred to be __strong.

这篇关于为了拥有ARC的强大性能,是否需要self.iVar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:50