Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While I can't think of a way to find out what keys will an object support you could use the fact that when you set the value for a nonexistent key the default behaviour of your object is to <a href="http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/KeyValueCoding/Concepts/BasicPrinciples.html#//apple_ref/doc/uid/20002170" rel="nofollow noreferrer">throw an exception</a>. You could enclose the <code>setValue:forKey:</code> method invocation in a <code>@try</code> / <code>@catch</code> block to handle these errors.</p> <p>Consider the following code for an object:</p> <pre><code>@interface KVCClass : NSObject { NSString *stuff; } @property (nonatomic, retain) NSString *stuff; @end @implementation KVCClass @synthesize stuff; - (void) dealloc { [stuff release], stuff = nil; [super dealloc]; } @end </code></pre> <p>This should be KVC-compliant for the key <code>stuff</code> but nothing else.</p> <p>If you access this class from the following program:</p> <pre><code>int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; KVCClass *testClass = [[KVCClass alloc] init]; [testClass setValue:@"this is the value" forKey:@"stuff"]; NSLog(@"%@", testClass.stuff); // Error handled nicely @try { [testClass setValue:@"this should fail but we will catch the exception" forKey:@"nonexistentKey"]; } @catch (NSException * e) { NSLog(@"handle error here"); } // This will throw an exception [testClass setValue:@"this will fail" forKey:@"nonexistentKey"]; [testClass release]; [pool drain]; return 0; } </code></pre> <p>You will get a console output similar to the following:</p> <pre><code>2010-01-08 18:06:57.981 KVCTest[42960:903] this is the value 2010-01-08 18:06:57.984 KVCTest[42960:903] handle error here 2010-01-08 18:06:57.984 KVCTest[42960:903] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&lt;KVCClass 0x10010c680&gt; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nonexistentKey.' *** Call stack at first throw: ( 0 CoreFoundation 0x00007fff851dc444 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x00007fff866fa0f3 objc_exception_throw + 45 2 CoreFoundation 0x00007fff85233a19 -[NSException raise] + 9 3 Foundation 0x00007fff85659429 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 434 4 KVCTest 0x0000000100001b78 main + 328 5 KVCTest 0x0000000100001a28 start + 52 6 ??? 0x0000000000000001 0x0 + 1 ) terminate called after throwing an instance of 'NSException' Abort trap </code></pre> <p>This shows that the first attempt to access the key <code>nonexistentKey</code> was nicely caught by the program, the second one generated an exception similar to yours.</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