【问题标题】:AppDelegate: this class is not key value coding-compliant for the key ---AppDelegate:此类不符合键的键值编码 ---
【发布时间】:2016-08-03 19:31:21
【问题描述】:

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.'

【问题讨论】:

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


    【解决方案1】:

    我相信它告诉你的是实话。您没有任何名为 googleDetails 的属性。

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

    static NSString *kGoogleDetails = @"googleUserDetailsDictionary";
    

    【讨论】:

    • 成功了! :) 但我有一个疑问。我读过只有 NSObject 的子类在内部符合NSKeyValueCoding 协议,而对于其他类,我们应该手动设置它。但我实际上并没有为 UIAppDelegate 这样做,但它仍然有效!怎么样?
    • 如果你愿意回答这个问题:stackoverflow.com/questions/38758112/…
    • 当你在 Objective-C 中工作时,大多数类都是 NSObject 的子类。 (检查 AppDelegate.h 中的类声明。)它在 Swift 中变得更加令人担忧。
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多