Cocoa中的KVC如何检查实例变量是否可访问?
最近我在读《可可设计模式》。在谈论 KVC 时,它说“KVC 首先检查名为 -key> 或 -getKey> 的访问器是否存在,如果不存在,它将尝试名为 <key> 或 _key> 的实例变量。 obj-c运行时可以检查实例变量是否存在吗?我认为这只能在编译时完成...
任何答案都很感激^_^
Recently i was reading "Cocoa Design Patterns". When talking about KVC, it said "KVC check if an accessor named -<key> or -get<Key> exists first, if not, it will try instance variable named <key> or _<key>".
Can obj-c runtime check whether an instance variable exist? I think it can only be done in compile time...
Any answers are appreciated ^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实可以。相关文档是Objective-C Runtime Reference ;具体来说,
class_getInstanceVariable
。文档遗漏的部分是,当类的实例没有此类变量时,该函数返回 NULL。据推测,KVC 将对象的类和候选变量名称传递给该函数,并且运行时为其提供
Ivar
的第一个名称就是它使用的名称。It can indeed. The relevant documentation for this is the Objective-C Runtime Reference; specifically,
class_getInstanceVariable
. The part the documentation leaves out is that that function returnsNULL
when instances of the class have no such variable.KVC, presumably, passes the object's class and the candidate variable names to that function, and the first name for which the runtime comes up with an
Ivar
is the one it uses.