3

If I have some instance of a class, say a UIView or subclass of it or just a simple subclass of NSObject, how can I get a list of the observable keypaths of that object?

I'd like to get a list of the valid keyPath values for the object to use them in the addObserver method.

  • Repost of http://stackoverflow.com/questions/754824/get-an-object-attributes-list-in-objective-c – Endemic Dec 06 '10 at 22:10
  • @Endemic: Thanks, I'll try that out. I didn't include "kvc" and "attribute" when I searched and that answer doesn't mention kvo or keypath. –  Dec 06 '10 at 22:17

1 Answers1

2

You cannot get a full list of kvo-compatible keys for an object. No such list exists. Not only is the object free to use whatever keys it wants, but it can even dynamically add new keys at runtime. The best you're likely to be able to do is to iterate over all the properties, treat their names as keys, and perhaps iterate over methods looking for anything of the form -setFoo: to treat "foo" as a key. But this is not foolproof, and it's not guaranteed to catch everything.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Kevin, thanks for the alerts and precautions. I'm not worried about dynamic keys for the moment. –  Dec 08 '10 at 15:34