6

Is there a method to find out if a class is key-value-compliant for a given key?

cfischer
  • 24,452
  • 37
  • 131
  • 214
  • Did you figure out an answer to this better that the one from Grady Player? As you can see, DanSkeel believes the answer isn't completely reliable. – daniel Oct 01 '22 at 03:05

1 Answers1

13

you can ask if it responds to the selector, or ask for the value for key

//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"]

but if you are getting a message that something isn't KVC compliant that usually means you have set something up incorrectly, a binding with the wrong key or class for instance.

Grady Player
  • 14,399
  • 2
  • 48
  • 76