iOS 当使用实例变量或 getter 方法时

发布于 2025-01-02 21:14:52 字数 358 浏览 3 评论 0原文

我有一个关于使用 getter 和实例变量的问题。让我们看一个例子。

假设我在 .h 文件中:

@property (nonatomic,strong) NSString *name

在 .m 文件中,我以这种方式合成该变量:

@synthesize name = _name;

之间有什么区别?

[self.name aMethod]

现在我的问题是: use:和

[_name aMethod]

Thanks!

i have a question about using getters and instance variables. Let's see an example.

Suppose i have in a .h file:

@property (nonatomic,strong) NSString *name

and in the .m file i synthesize that variable in this way:

@synthesize name = _name;

Now my question is: what's the difference between use:

[self.name aMethod]

and

[_name aMethod]

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛装女皇 2025-01-09 21:14:52

第一个通过 getter 方法访问 ivar。第二个直接访问ivar。由于它是一个简单的合成属性,因此除了第一个进行额外的方法调用之外没有太大区别。但是,如果该属性是原子的或动态的,或者 getter 方法很复杂,那么就会存在差异,第一个属性实际上是原子的,而第二个则不是,并且第一个属性实际上会触发属性中的任何复杂逻辑。吸气剂,而第二个不会。

简而言之,编译器将第一个调用重写为:,

[[self name] aMethod]

而第二个调用则保持原样。

The first one accesses the ivar through the getter method. The second directly accesses the ivar. Since it's a simple, synthesized property, there's not much difference except that the first makes an additional method call. However, if the property were atomic, or dynamic, or the getter method were complicated, there'd be a difference in that the first one would actually be atomic while the second wouldn't and the first would actually trigger any complicated logic in the getter while the second wouldn't.

In simplest terms, the compiler re-writes the first call to:

[[self name] aMethod]

while the second call is simply left as-is.

烦人精 2025-01-09 21:14:52
[self.name aMethod]

相当于

[[self name] aMethod]

因此调用 getter 并将消息发送到其结果。

在你的情况下,可见的结果将是相同的。

然而,如果 getter 不是微不足道的(即合成的),情况可能就不是这样了。

[self.name aMethod]

is equivalent to

[[self name] aMethod]

Thus a getter is called and the message is sent to its result.

In your case, the visible result will be the same.

However, it may not be a case if getter wasn't trivial (i.e. synthesized).

梦中的蝴蝶 2025-01-09 21:14:52

第一个通过 getter 进行调用——它等于[[self name] aMethod]。第二个仅使用直接访问。

通常您应该倾向于使用访问器,但有时您应该偏离这一点。最常见的情况是在部分构造状态下,例如初始化程序和dealloc。原因是您应该小心地构建或销毁您的状态,而不是对对象的接口语义感兴趣 - 也就是说,使用访问器可能会产生负面的行为和语义副作用。

更完整的原因列表可以在这里找到:
为什么要使用 ivar?

The first calls through the getter -- it is equal to [[self name] aMethod]. The second just uses direct access.

You should generally favor using the accessors, but there are times where you should deviate from that. the most common occurrence is while in partially constructed states, such as your initializer and dealloc. the reason is that you should be carefully constructing or destroying your state, and not interested your object's interface semantics - that is, using the accessors can have negative behavioral and semantic side effects.

A more complete list of reasons can be found here:
Why would you use an ivar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文