我难以从IOS中的属性列表中对键进行排序。
我将列表加载到字典中,然后将密钥提取到使用metode对数组进行排序的数组:“sortedArrayUsingSelector'selector(compare :)”
当我尝试使用这些键时,这些值似乎没有按照那里的键进行排序? 例如:(键值vallues) 未分类:资产0,欧登塞1,尼堡1,米德尔法特0。 排序:Assens,1,Odense 1,Nyborg 0,Middelfart 0。
`
NSBundle *bundle = [NSBundle mainBundle];
NSURL *KSplistURL = [bundle URLForResource:@"KommunerStedtillæg"
withExtension:@"plist"];
NSDictionary *dictionary = [NSDictionary
dictionaryWithContentsOfURL:KSplistURL];
self.kommuner = dictionary;
//NSArray *keys = [[self.kommuner allKeys] sortedArrayUsingSelector:@selector(compare)];
NSArray *components = [self.kommuner allKeys];
NSArray *keys = [components sortedArrayUsingSelector:@selector(compare:)];
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
for (id key in keys)[newDict setObject:[dictionary objectForKey:key]forKey:key];
self.kommunerKeys = [newDict allKeys];
self.kommunerValues = [newDict allValues];
`
答案 0 :(得分:2)
为什么你认为字典会知道你正在对其关键数组的无关副本进行排序?你必须创建另一个这样的字典:
NSArray *keys = [[originalDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
for (id key in keys)
[newDict setObject:[originalDict objectForKey:key] forKey:key];
现在newDict
包含已排序的键和值。