Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no way, in general, to determine if an object has a value for a given key. An instance may decide to return a value for an otherwise undefined key from its <code>-valueForUndefinedKey:</code> method. Or it may let the default implementation throw an exception. Modern Objective-C (2.0) objects <em>often</em> declare relevant properties in their public API. For these objects, you can use the Objective-C runtime's <code>class_copyPropertyList</code> or <code>protocol_copyPropertyList</code> to get a list of the available properties. You can also emulate the KVC search list, testing via <code>repondsToSelector:</code> if the instance responds to an appropriate getter method. Finally you could use the runtime's <code>class_copyIvarList</code> to check the ivars for an appropriate ivar. This is a <em>lot</em> of work that you <strong>shouldn't be doing</strong>. It won't guarantee that you know whether an object instance will raise an exception when sent a <code>valueForKey:</code> message and it indicates a bigger issue...</p> <p>If you have a collection of objects that must all provide a value for a given key, but some don't, you have a design problem. You should define an appropriate interface (a <code>@protocol</code> in Objective-C) that declares the needed properties. You can then test for conformance to this protocol, or make use of the compiler to do some compile-time type checking for you to make sure instance conform to the protocol (if you're passing around single instances).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload