我应该在Objective-C中使用ivars吗? [英] Should I use ivars in Objective-C?

查看:101
本文介绍了我应该在Objective-C中使用ivars吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仅使用@properties的应用程序。我的任何课程档案中​​都没有宣布任何一个ivar。据我所知,随着@property的推出,不再需要ivars了。我是按照最佳做法编码的吗?从长远来看,这最终会在众所周知的屁股中咬我吗?我一直在阅读关于正确和错误的评论......

I have an application that I'm writing that uses solely @properties. I have not one ivar declared at all in any of my class files. As I understand it ivars are no longer needed with the introduction of @property. 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"...

推荐答案

我一般不会申报ivars,要么。 我会经常使用 @synthesize foo = foo _; 但是当我指的是通过方法时反对直接访问,反之亦然。我总是让编译器自动使用 _ 前缀合成ivar(根据 struck 短语防止意外直接访问)。

I generally don't declare ivars, either. I will often use @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).

而且,正如Caleb说的那样,仍然存在一些伊势,你只是没有明确地声明它们,除非你真的想要(事实上,你并没有像如果您的API设计得恰当,那么标题对该类的客户端没有用。)

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).

我也发现炒作仅使用init / dealloc中的直接访问,在其他地方使用setter / getter在很大程度上被夸大了,因此,只需在任何地方使用setter / getter。现实情况是,如果你在初始化/解除分配期间有观察员,你已经被哄骗了;根据定义,在构造/破坏期间,对象的状态是未定义的,因此,观察者不可能正确地对该状态作出正确的推理。

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. 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.

正如Caleb所指出的,在init / dealloc中使用直接ivar访问的另一个原因是避免实现自定义setter / getter逻辑的子类,这些逻辑可能会因init / dealloc期间对象的未定义状态而变为barf 。

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.

虽然这可能是真的,但我认为用自定义行为实现setter / getter是一个令人讨厌的架构缺陷。这样做很脆弱,因此随着时间的推移重构代码变得非常困难。同样,这种自定义行为通常会依赖于对象中的其他状态,然后依赖性会导致对状态更改的顺序依赖性,而这些状态更改完全没有被看似简单的 @property 声明。

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.

即如果您的setter和getter被编写为 foo.bar = bad; 无法在任何时间执行 foo ,然后你的代码被破坏了。

I.e. if your setters and getters are written such that foo.bar = bad; cannot be executed at any time on foo, then your code is busted.

这篇关于我应该在Objective-C中使用ivars吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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