id sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@1, @2, @3]];
NSMutableDictionary *test = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet];
test[@4] = @"First element (not in the shared key set, but will work as well)";
NSDictionary *immutable = [test copy];
NSParameterAssert(immutable == 1);
((NSMutableDictionary *)immutable)[@5] = @"Adding objects to an immutable collection should throw an exception.";
NSParameterAssert(immutable == 2);
上面代码断言那里直接用一个字典对象和1进行比较,immutable == 1 这种用法是判断immutable里的value个数么?还是其他什么的?
1楼 @nonomori 这个是objc.io里第7章关于讲字典那块的,他是这么说的:Shared-key dictionaries are always mutable, even when calling ‘copy’ on them. This behavior is not documented but can be easily tested (共享键字典始终是可变的,即使对它们执行了”copy”命令后。这个行为文档中并没有说明,但很容易被测试)。
我在xcode里试了下有warning,运行的话这个断言也必然是false...不知道作者这样写是啥意图...