Why getting error: class is not key value coding-compliant for the key name?

Why getting error: class is not key value coding-compliant for the key name?

正如 Xcode 中建议的那样,在 objective c 中使用以下代码自动完成建议会产生运行时错误,

for (int i = 1; i <= 10; i++)
{
    NSLog(@"%d", i);

    NSDictionary *dic = [[NSDictionary alloc] init];
    [dic setValue:@"Alex" forKey:@"name"];
    [dic setValue:@"45" forKey:@"Age"];
}

2020-03-29 21:55:34.588607+0600 Test[5299:15492347] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x7fff806228e0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'

我错过了什么?

更新

正如 matt 评论的那样 NSDictionary 是不可变的,那么,

  1. 为什么 Xcode 的自动完成建议中的 NSDictionary 可以使用 setValue 方法?

  2. 为什么不能在编译时而不是运行时检测到修改非可变集合的方法?

我通过将 setValue 更改为 setObject

来解决问题
NSDictionary *dic = [[NSDictionary alloc] init];
[dic setObject:@"Alex" forKey:@"name"];
[dic setObject:@"45" forKey:@"Age"];