查找某个类是否与给定键的键值兼容

cfi*_*her 4 cocoa cocoa-touch objective-c key-value-observing key-value-coding

有没有一种方法可以找出某个类是否符合键值的特定键?

Gra*_*yer 12

您可以询问它是否响应选择器,或询问键的值

//will check for the getter
[anObj respondsToSelector:@selector(someKey)]

//will check in a way that doesn't throw an exception for a value
[andObj valueForKey:@"someKey"]

//keypath of a nested value
[anObj valueForKeypath:@"child.property"]
Run Code Online (Sandbox Code Playgroud)

但是如果你收到的消息是某些东西不符合KVC,这通常意味着你设置错误,例如绑定了错误的密钥或类.