本文介绍了NSDictionary订单与allKeys订单不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照第一个字母组织的名称创建了排序数组的NSDictionary(参见下面的结果)。当我对同一个字典使用命令allKeys时,顺序不一样。我需要相同的顺序,因为这个NSDictionary在UITableview中使用,应该是按字母顺序排列的。

I've created NSDictionary of sorted arrays by name organized by first letter (see results below). When I use the command allKeys for that same Dictionary, the order is not the same. I need the order the same because this NSDictionary is used in UITableview and should be alphabetical.

 - (NSDictionary*) dictionaryNames {

NSDictionary *dictionary;
NSMutableArray *objects = [[NSMutableArray alloc] init];

NSArray *letters = self.exhibitorFirstLetter;
NSArray *names = self.exhibitorName;

for (NSInteger i = 0; i < [self.exhibitorFirstLetter count]; i++)
{
    [objects addObject:[[NSMutableArray alloc] init]];
}

dictionary = [[NSDictionary alloc] initWithObjects: objects forKeys:letters];

for (NSString *name in names) { 
    NSString *firstLetter = [name substringToIndex:1];

    for (NSString *letter in letters) {  //z, b
        if ([firstLetter isEqualToString:letter]) {
            NSMutableArray *currentObjects = [dictionary objectForKey:letter];
            [currentObjects addObject:name];
        }
    }

}

    NSLog(@"%@", dictionary);
NSLog(@"%@", [dictionary allKeys]);
return dictionary;

}

 B =     (
    "Baker's Drilling",
    "Brown Drilling"
);
C =     (
    "Casper Drilling"
);
J =     (
    "J's, LLC"
);
N =     (
    "Nelson Cleaning",
    "North's Drilling"
);
T =     (
    "Tim's Trucks"
);
Z =     (
    "Zach's Main",
    "Zeb's Service",
    "Zen's"
);

}

J,
T,
B,
N,
Z,
C

推荐答案

NSDictionary不是有序集合。没有办法控制它的订购方式,它可能会根据操作系统版本,设备类型和字典内容完全改变。

NSDictionary is not an ordered collection. There's no way to control how it orders things, and it may change completely depending on OS version, device type, and dictionary contents.

这篇关于NSDictionary订单与allKeys订单不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:55