Search code examples
iosobjective-cappdelegatekey-value-coding

AppDelegate: this class is not key value coding-compliant for the key ---


In AppDelegate.h I created the following property,

@property (strong, nonatomic) NSMutableDictionary *googleUserDetailsDictionary;

In AppDelegate.m,

static NSString *kGoogleDetails = @"googleDetails"; @synthesize googleUserDetailsDictionary;

I've made a custom method in AppDelegate.m in which I added the following line: [self setValue:googleUserDetailsDictionary forKey:kGoogleDetails];

In the ViewController which I wanted to be a observer for googleUserDetailsDictionary property,

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate addObserver:self forKeyPath:@"googleUserDetailsDictionary" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:kGoogleDetails]) {
        NSLog(@"This is change taking place: %@ \t\t", change);

    }
}

Then I get the following Error JUST WHEN I TRY TO OPEN THE ViewController: ** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FIRA_AppDelegate-1470253453284 0x7fd4fa839410> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key googleDetails.'


Solution

  • I believe it's telling you the truth. You don't have any property called googleDetails.

    Try changing the key path to match the property, such as:

    static NSString *kGoogleDetails = @"googleUserDetailsDictionary";