如何找到符合 KVC 的 Objective-C 对象的所有属性键?
是否有一种方法可以返回符合 NSKeyValueCoding 协议的对象的所有键?
类似 [object getPropertyKeys]
的内容将返回 NSString 对象的 NSArray。 它适用于任何符合 KVC 的对象。 有这样的方法吗? 到目前为止,我在搜索 Apple 文档时还没有找到任何内容。
谢谢, G。
Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol?
Something along the lines of [object getPropertyKeys]
that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far.
Thanks,
G.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 class_getPropertyList。 这将告诉您该对象的所有
@properties
。它不一定会列出每个符合 KVC 的属性,因为任何不带参数并返回值的方法都是有效的符合 KVC 的 getter。 运行时没有 100% 可靠的方法来知道哪些行为作为属性(例如,
-[NSString length]
)以及哪些行为作为命令(例如,-[NSFileHandle readDataToEndOfFile] )。
无论如何,您应该将符合 KVC 的属性声明为
@properties
,因此这应该不是什么大问题。Use class_getPropertyList. That will tell you all the
@properties
of the object.It won't necessarily list every KVC-compliant property, because any method that takes no arguments and returns a value is a valid KVC-compliant getter. There's no 100%-reliable way for the runtime to know which ones behave as properties (e.g.,
-[NSString length]
) and which ones behave as commands (e.g.,-[NSFileHandle readDataToEndOfFile]
).You should be declaring your KVC-compliant properties as
@properties
anyway, so this shouldn't be too big of a problem.不存在这样的方法,因为 KVO 系统不需要对象/类向其注册它们支持 KVO 的属性。 任何密钥都可能支持 KVO,唯一了解的方法是通过作者的文档。
当然,不能保证
@property
将支持 KVO; 很可能编写一个不需要的属性(有时可能是必要的)。 因此,在我看来,获取类的@property
列表,然后假设它们符合 KVO 将是一个危险的选择。There is no such method as the KVO system does not require objects/classes to register with it which properties they support KVO for. Any key could potentially support KVO, the only way to know is from the author's documentation.
And of course, there is no guarantee that an
@property
will support KVO; it's quite possible to write a property that doesn't (and may be necessary sometimes). So, getting a list of a class's@property
s and then assuming they're KVO-compliant would be a dangerous choice in my opinion.您需要一个 getPropertyType 函数。 请参阅这篇文章:在 Objective- 中获取对象属性列表 - C
You need a getPropertyType function. See this post: Get an object attributes list in Objective-C
对于 Swift 旁观者,您可以通过利用
Encodable
功能来获得此功能。 我将解释如何:使您的对象符合
Encodable
协议为
Encodable
创建扩展以提供toDictionary
功能在对象实例上调用
toDictionary
并访问keys
属性。瞧! 像这样访问您的属性:
For Swift onlookers, you can get this functionality by utilising the
Encodable
functionality. I will explain how:Conform your object to
Encodable
protocolCreate extension for
Encodable
to providetoDictionary
functionalityCall
toDictionary
on your object instance and accesskeys
property.Voila! Access your properties like so: