键值编码和方法调用
这是一个关于 Cocoa 良好编程技术的问题。
当你想调用类的一个属性上的方法时,你应该使用 KVC 来获取接收者还是只输入属性的名称?
示例,KVC:
[[self property] myMethod];
示例,简单:
[property myMethod];
谢谢!
It's a question about good programming techniques with Cocoa.
When you want to call a method on one property of your class, should you use KVC to get the receiver or just put the name of your property?
Example, KVC:
[[self property] myMethod];
Example, simple:
[property myMethod];
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那不是 KVC。 KVC 的方式是:
当你在编译时知道属性时,没有理由这样做;您可以直接询问属性值或 ivar 值。由于 KVO 和(在 Mac 上)绑定已经实现,没有太多理由直接使用 KVC,因为 KVO 和绑定会为您使用它。
不访问该属性;它访问 ivar。
仅当您向属性的持有者(示例中的
self
)发送访问者消息时,您才访问该属性。使用[self property]
还是self.property
并不重要,因为它们是等效的;任何一个都是发送给self
的property
消息,具有任何暗示的副作用。这是关键的区别:点击访问器可能会导致副作用,而直接访问 ivar 则永远不会。
因此,最佳实践是:在所有实例方法中使用该属性(因为您可能想要访问器的副作用),除了
init
方法和dealloc< /code>,副作用将是一件坏事。 (作为一般规则,您不应向半初始化或半释放的对象发送消息。例外情况是,当您明确将该方法注释为 init/dealloc 过程的一部分,因此将其编写为可以安全地在这样的情况。)
That isn't KVC. The KVC way is:
There is no reason to do this when you know the property at compile time; you can just ask for the property value or the ivar value directly. With KVO and (on the Mac) Bindings already implemented, there's not much reason to use KVC directly, as KVO and Bindings use it for you.
That doesn't access the property; it accesses the ivar.
You're only accessing the property when you send an accessor message to the property's holder (
self
in your examples). It doesn't matter whether you use[self property]
orself.property
, as they're equivalent; either one is aproperty
message toself
, with whatever side effects that implies.That's the key difference: Hitting the accessor may cause side effects, whereas accessing the ivar directly never will.
Hence, the best practice: Use the property in all your instance methods (as you probably want the accessors' side effects), except in
init
methods anddealloc
, where side effects would be a bad thing. (As a general rule, you should not send messages to a half-initialized or half-deallocked object. The exception is when you explicitly commented the method as being part of your init/dealloc process and therefore wrote it to be safe to use in such circumstances.)我相信正式版本在技术上是正确的,因为这将保证时髦吸气剂产生任何副作用。 (为了确保这一点,请创建一个包含
NSLog("in getter!")
的自定义 getter,并让我们知道它是否有效。)对于设置,您必须使用
[self setProperty:foo ];
asproperty = foo
绕过 setter 并可能导致内存泄漏。如果您感觉更自然,则点符号(例如,
self.property
和self.property = foo
)与[self property] 和<代码>[self setProperty:foo]。
I believe the formal version is technically correct as that will guarantee any side-effects from a funky getter. (To make sure, make a custom getter that includes
NSLog("in getter!")
and let us know if it works.)For setting you have to use the
[self setProperty:foo];
asproperty = foo
bypasses the setter and can lead to memory leaks.If it feels more natural to you, the dot notation (e.g.,
self.property
andself.property = foo
) is identical to[self property]
and[self setProperty:foo]
.