为什么不做下面的NSDictionary
/NSMutableDictionary
调用会产生错误或警告吗?
我预计这里会出现错误,因为 rhsNSDictionary
文字与泛型类型不匹配NSDictionary
lhs 局部变量。
NSDictionary<NSString *, NSNumber *> *foo = @{ @(42) : @"foo" };
我预计这里会出现错误,因为密钥类型与NSMutableDictionary
的关键泛型类型:
NSMutableDictionary<NSString *, NSNumber *> *foo = [NSMutableDictionary new];
// neither of these calls produces an error. :(
foo[@(42)] = @(42);
[foo setObject:@(42) forKey:@(42)];
当我尝试分配一个不正确类型的值时,我看到一个错误,所以我知道泛型错误在某种程度上起作用:
NSMutableDictionary<NSString *, NSNumber *> *foo = [NSMutableDictionary new];
foo[@"foo"] = @"bar";
导致出现以下警告:
Foo.m:81:16: Incompatible pointer types sending 'NSString *' to parameter of type 'NSNumber * _Nullable'
为什么文字赋值或键入不正确的键不会导致警告/错误?
我将其归档为雷达。 http://www.openradar.me/25060179