有序字典。枚举顺序与条目添加顺序一致。如果某个条目被覆盖,顺序保持不变。
例如,
#import <GHODictionary/GHODictionary.h>
GHODictionary *dict = [GHODictionary dictionary];
dict[@"key1"] = @(1);
dict[@"key2"] = @(2);
dict[@"key1"] = @(3);
for (id key in dict) ... // @"key1", @"key2"
[dict allKeys]; // The same as enumeration, @"key1", @"key2"
[dict map:^(id key, id value) { ... }]; // (@"key1", @(3)), (@"key2", @(2))
如果您想要覆盖一个值并将其移到排序的末尾,那么请将其移除然后重新添加
dict[@"key1"] = nil;
dict[@"key1"] = @(3);
[dict allKeys]; // @"key2", @"key1"
因为它是有序的,所以它也可以进行排序
dict[@"b"] = @(2);
dict[@"c"] = @(3);
dict[@"a"] = @(1);
[dict sortKeysUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[dict allKeys]; // @"a", @"b", @"c"
pod "GHODictionary"
github "gabriel/GHODictionary"