键值编码和方法调用

发布于 2024-09-10 22:57:33 字数 223 浏览 7 评论 0原文

这是一个关于 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 技术交流群。

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

发布评论

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

评论(2

不奢求什么 2024-09-17 22:57:33

示例,KVC:

[[自身属性] myMethod];

那不是 KVC。 KVC 的方式是:

[[self valueForKey:@"myProperty"] myMethod]

当你在编译时知道属性时,没有理由这样做;您可以直接询问属性值或 ivar 值。由于 KVO 和(在 Mac 上)绑定已经实现,没有太多理由直接使用 KVC,因为 KVO 和绑定会为您使用它。

示例,简单:

[属性 myMethod];

不访问该属性;它访问 ivar。

仅当您向属性的持有者(示例中的 self)发送访问者消息时,您才访问该属性。使用 [self property] 还是 self.property 并不重要,因为它们是等效的;任何一个都是发送给 selfproperty 消息,具有任何暗示的副作用。

这是关键的区别:点击访问器可能会导致副作用,而直接访问 ivar 则永远不会。

因此,最佳实践是:在所有实例方法中使用该属性(因为您可能想要访问器的副作用),除了 init 方法和 dealloc< /code>,副作用将是一件坏事。 (作为一般规则,您不应向半初始化或半释放的对象发送消息。例外情况是,当您明确将该方法注释为 init/dealloc 过程的一部分,因此将其编写为可以安全地在这样的情况。)

Example, KVC:

[[self property] myMethod];

That isn't KVC. The KVC way is:

[[self valueForKey:@"myProperty"] myMethod]

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.

Example, simple:

[property myMethod];

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] or self.property, as they're equivalent; either one is a property message to self, 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 and dealloc, 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.)

执手闯天涯 2024-09-17 22:57:33

我相信正式版本在技术上是正确的,因为这将保证时髦吸气剂产生任何副作用。 (为了确保这一点,请创建一个包含 NSLog("in getter!") 的自定义 getter,并让我们知道它是否有效。)

对于设置,您必须使用 [self setProperty:foo ]; as property = foo 绕过 setter 并可能导致内存泄漏。

如果您感觉更自然,则点符号(例如,self.propertyself.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]; as property = foo bypasses the setter and can lead to memory leaks.

If it feels more natural to you, the dot notation (e.g., self.property and self.property = foo) is identical to [self property] and [self setProperty:foo].

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