简体   繁体   English

为什么自动生成的iOS Apple代码使用实例变量而不是属性?

[英]Why does auto-generated iOS Apple code use instance variables instead of properties?

I did a lot of iOS development pre-ARC. 我做了很多iOS开发前的ARC。 Now with ARC I've noticed that Apple generated code is using instance variables instead of properties. 现在有了ARC,我注意到Apple生成的代码使用的是实例变量而不是属性。 Why is that? 这是为什么?

For instance a master-detail view controller - on the DetailView we see this: 例如,一个主 - 细节视图控制器 - 在DetailView上我们看到:

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
    _detailItem = newDetailItem;

    // Update the view.
    [self configureView];
}
}

Why not self.detailItem = newDetailItem;? 为什么不self.detailItem = newDetailItem;? When I first learned iOS dev the use of properties was the recommended practice. 当我第一次学习iOS开发时,建议使用属性。

It would create a infinitive recursive loop. 它会创建一个不定式的递归循环。 Since self.detailItem = obj; 由于self.detailItem = obj; calls: 要求:

- (void)setDetailItem:(id)newDetailItem

Regarding the "_" it's how the variable is named when you don't syntersize it. 关于“_”,当你不同化它时,它是如何命名变量的。 You can change it if you want, although i see no reason to do that. 如果你愿意,你可以改变它,虽然我认为没有理由这样做。

self.detailItem = newDetailItem; IS [self setDetailItem:newDetailItem]; IS [self setDetailItem:newDetailItem];

So that would cause a stack overflow. 这样会导致堆栈溢出。

When you declare a property it already defines the getter and the setter for an instance variable (unless it's in a category). 声明属性时,它已经为实例变量定义了getter和setter(除非它在一个类别中)。 So if you have to customize either the getter or the setter then you need to access the instance variable directly. 因此,如果您必须自定义getter或setter,则需要直接访问实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM