AppDelegate:这个类不是密钥值编码兼容的密钥---

时间:2016-08-03 19:31:21

标签: ios objective-c appdelegate key-value-coding

AppDelegate.h中,我创建了以下属性

@property (strong, nonatomic) NSMutableDictionary *googleUserDetailsDictionary;

AppDelegate.m

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

我在AppDelegate.m中创建了一个自定义方法,其中添加了以下行: [self setValue:googleUserDetailsDictionary forKey:kGoogleDetails];

在我希望成为googleUserDetailsDictionary属性的观察者的ViewController中,

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);

    }
}

然后我得到以下错误,当我尝试打开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.'

1 个答案:

答案 0 :(得分:1)

我相信它说的是实话。您没有任何名为change的属性。

尝试更改密钥路径以匹配属性,例如:

sed "s/.*u'longitude': \([^,]\+\).*u'latitude': \([^,]\+\).*/\1 \2/g" a.json