NSMutableDictionary 字面量

NSMutableDictionary Literal

CLBeaconRegion *region;
NSMutableDictionary *beacons;
....

beacons[region] = beacons;

上面的代码是否将 beacons 键设置为 regionbeacons 的值?我认为密钥必须是 NSDictionary/NSMutableDictionary 中的 string

当你说:

beacons[region] = beacons;

这等同于:

[beacons setObject:beacons forKey:region];

第一个只是第二个的语法快捷方式。无论哪种方式,它可能都不是您想要的,因为设置指向字典本身的字典 key/value 对 通常 没有意义。你到底想做什么?

字典中的键不一定是字符串,但它必须符合 NSCopying protocol, see the definition:

- (void)setObject:(id)anObject
           forKey:(id<NSCopying>)aKey

beacons[region] = beacons; 似乎不正确,因为您在键 region.

下将 beacons 字典添加到自身

请注意,使用键值编码时,键必须是字符串,请参阅 Key-Value Coding Fundamentals