gpt4 book ai didi

iphone - 循环遍历已排序的字典,将其取消排序。

转载 作者:行者123 更新时间:2023-11-28 18:39:31 27 4
gpt4 key购买 nike

我有一本已排序的字典。我的问题是当我遍历它时它再次变得未排序。

    //sort the poiIDArray so it appears alphabetically
NSArray *sortedValues = [[self.pois allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *orderedDictionary=[[NSMutableDictionary alloc]init];
for(NSString *valor in sortedValues){
for(NSString *clave in [self.pois allKeys]){
if ([valor isEqualToString:[self.pois valueForKey:clave]]) {
[orderedDictionary setValue:valor forKey:clave];
}
}
}

NSLog(@"ordered dic: %@",orderedDictionary);


//loop through dic and get names.
for(NSString *key in orderedDictionary) {

NSLog(@"%@",key);

}

这是我的日志。

2012-10-26 11:49:35.221 CPOP Test 4[1277:c07] ordered dic: {
"Badger Burgers" = 5;
"Costa Coffee" = 4;
"Spiffing Drinks" = 6;
"Vals Vegetables" = 7;
}
2012-10-26 11:49:35.221 CPOP Test 4[1277:c07] Costa Coffee
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Spiffing Drinks
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Badger Burgers
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Vals Vegetables

谁能告诉我为什么会这样以及如何解决?

最佳答案

字典根据定义是无序的。如果您需要特定顺序,请使用数组。

关于iphone - 循环遍历已排序的字典,将其取消排序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13085706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com