简体   繁体   English

我应该在Objective-C中使用ivars吗?

[英]Should I use ivars in Objective-C?

I have an application that I'm writing that uses solely @properties. 我有一个正在编写的仅使用@properties的应用程序。 I have not one ivar declared at all in any of my class files. 我的任何类文件中都没有声明过一个ivar。 As I understand it ivars are no longer needed with the introduction of @property. 据我了解,@ property的引入不再需要ivars。 Am I coding according to best practice? 我是否根据最佳做法进行编码? Will this end up biting me in the proverbial butt in the long term? 从长远来看,这最终会在我的屁股上咬我吗? I have been reading mixed reviews on what is "right" and "wrong"... 我一直在阅读关于什么是“正确”和“错误”的混合评论。

It's not so much that instance variables aren't needed. 不需要实例变量不是很多。 It's just that instance variable declarations aren't needed. 只是不需要实例变量声明 Given a property and a @synthesize statement, the compiler will take care of creating the instance variable along with appropriate accessor methods. 给定一个属性和一个@synthesize语句,编译器将负责创建实例变量以及适当的访问器方法。

There's nothing wrong with using properties exclusively. 完全使用属性没有错。 They simplify memory management. 它们简化了内存管理。 There's also nothing wrong with using iVars without properties, if that's what you want. 如果这就是您想要的,使用不带属性的iVars也没有错。 If you want to use properties but don't want to advertise the accessors to the rest of the world (ie maintain encapsulation), consider declaring your non-public properties in a class extension (basically an anonymous category in your implementation file). 如果要使用属性,但又不想将访问器广告发布到世界其他地方(即保持封装),请考虑在类扩展(在实现文件中基本上是匿名类别)中声明非公共属性。

I generally don't declare ivars, either. 我通常也不会声明ivars。 I will often use @synthesize foo = foo_; 我将经常使用 @synthesize foo = foo_; though to prevent direct access when I meant through-method or vice-versa. 虽然在我指的是直通方法时会阻止直接访问,反之亦然。 And I always let the compiler automatically synthesize the ivar with the _ prefix (which prevents accidental direct access, as per the struck phrase). 而且,我总是让编译器自动使用_前缀来合成ivar(根据被 删除的 短语,这可以防止意外的直接访问)。

And, as Caleb said, there are still ivars floating about, you just don't explicitly declare 'em unless you really want to (which, really, you don't as exposed ivars in the headers are not useful to clients of the class, if your API is designed appropriately). 而且,正如Caleb所说的那样,仍然有很多ivars,除非您确实想要(除非您在标头中公开的ivars对类的客户没有用,否则除非您明确声明,否则就不要显式声明它们)。 ,如果您的API设计合理)。

I also find that the hype over "only use direct access in init/dealloc, use setter/getter everywhere else" to be largely overblown and, thus, just use the setter/getter everywhere. 我还发现,对“仅在init / dealloc中使用直接访问,在其他所有地方使用setter / getter”的炒作在很大程度上被夸大了,因此,只在各处使用setter / getter。 The reality is that if you have observers during initialization/deallocation, you are already hosed; 现实情况是,如果在初始化/解除分配期间有观察者,那么您已经准备就绪; the state of the object is, by definition, undefined during construction/destruction and, thus, an observer can't possibly reason correctly about the state. 根据定义,对象的状态在构造/销毁期间是未定义的,因此观察者可能无法正确推断该状态。


As Caleb points out, another reason to use direct ivar access in init/dealloc is to avoid subclasses that implement custom setter/getter logic that may barf due to the undefined state of the object during init/dealloc. 正如Caleb指出的,在init / dealloc中使用直接ivar访问的另一个原因是,避免实现自定义setter / getter逻辑的子类可能由于init / dealloc过程中对象的未定义状态而阻塞。

While this may be true, I consider it a nasty architectural flaw to implement setters/getters with custom behavior. 虽然这可能是正确的,但我认为以自定义行为实现设置者/获取者是一个讨厌的体系结构缺陷。 Doing so is fragile and makes it significantly more difficult to refactor the code over time. 这样做很脆弱,并且随着时间的推移重构代码变得更加困难。 As well, such custom behavior will often have dependency on other state within the object and that dependency then leads to order dependencies on state changes that are not at all reflected by the seeming simple @property declaration. 同样,这种自定义行为通常会依赖于对象内的其他状态,然后这种依赖导致对状态更改的顺序依赖,而这些似乎根本不由简单的@property声明反映出来。

Ie if your setters and getters are written such that foo.bar = bad; 即,如果您的setter和getter编写为foo.bar = bad; cannot be executed at any time on foo , then your code is busted. 无法随时foo上执行,则您的代码将被破坏。

当然,使用iVars并没有错,但是最佳实践现在确实要求使用@property。

One place where you may want to use an ivar is when you want to declare a protected property. 您可能要使用ivar的一个地方是要声明一个受保护的属性。 You declare the property in the .m file for a class, and declare its corresponding ivar in the .h with the @protected directive. 您可以在.m文件中为类声明该属性,并使用@protected指令在.h中声明其对应的ivar。 This will then let you have a protected access in the subclass. 然后,这将使您在子类中具有受保护的访问。 There's no alternative for protected access to members. 没有其他选择可以保护成员的访问。

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

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