ios – 将NSArray转换为NSDictionary

我想将一个nsarray转换为我使用的nsdictionary

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array { id objectInstance; NSUInteger indexKey = 0; NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; for (objectInstance in array) [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]]; return (NSDictionary *)[mutableDictionary autorelease]; } 

输出结果是:

 { 0 = { Event = ""; ID = 1; }; 3 = { Event = ""; ID = 77; }; 2 = { Event = ""; ID = 23; }; 1 = { Event = ""; ID = 45; }; 7 = { Event = ""; ID = 10; }; 5 = { Event = ""; ID = 26; }; 6 = { Event = ""; ID = 27; }; 8 = { Event = ""; ID = 28; }; } 

在转换为nsdictionary后,nsdictionary的顺序与原始顺序不一致,我想在nsarray中显示相同的顺序,我不知道如何? 你可以帮我吗?

NSDictionary没有订单。 对键进行sorting并使用它们来访问条目。

如果我从你对@ACB和@Zaph的评论中正确理解了你的意见,你可以这样做:

维护一个将整数键映射到按键sorting的对象值的集合。

如果我的理解正确,数组不足以达到您的目的,因为数组中的整数键不允许出现“漏洞”。 然而,你需要考虑到漏洞:在你的问题的输出中,缺less了4的键值对。 出于这个原因,字典是吸引你的。

不幸的是,字典不允许你保持它所包含的键值对的sorting,就像@Zaph指出的那样。 然而,你说,你只是想显示在UITableView按键sorting的字典中的值。 据推测,只要词典的内容以正确的顺序显示在表格视图中,字典序列化到磁盘的顺序就不重要了(使用writeToFile:atomically:

字典可以用于这个目的如下。 首先,我们需要一个类PFXKeyValuePair ;

 @interface PFXKeyValuePair : NSObject @property (nonatomic) id<NSCopying> key; @property (nonatomic) id value; + (PFXKeyValuePair *)pairWithValue:(id)value forKey:(id<NSCopying>)key; + (NSArray *)pairsWithValues:(NSArray *)values forKeys:(NSArray *)keys; @end @implementation PFXKeyValuePair + (PFXKeyValuePair *)pairWithValue:(id)value forKey:(id<NSCopying>)key { PFXKeyValuePair *pair = [[PFXKeyValuePair alloc] init]; pair.value = value; pair.key = key; return pair; } + (NSArray *)pairsWithValues:(NSArray *)values forKeys:(NSArray *)keys { NSAssert(values.count == keys.count, @"The array of values must be the same size as the array of keys."); NSUInteger count = values.count; NSMutableArray *mutableRes = [NSMutableArray array]; for (NSUInteger index = 0; index < count; index++) { PFXKeyValuePair *pair = [PFXKeyValuePair pairWithValue:values[index] forKey:keys[index]]; [mutableRes addObject:pair]; } return [mutableRes copy]; } @end 

其次,我们需要NSDictionary上的类别方法:

 @interface NSDictionary (PFXAdditions) - (NSArray *)pfx_keyValuePairsSortedByKeyUsingComparator:(NSComparator)comparator; @end @implementation NSDictionary (PFXAdditions) - (NSArray *)pfx_keyValuePairsSortedByKeyUsingComparator:(NSComparator)comparator { NSArray *sortedKeys = [self.allKeys sortedArrayUsingComparator:comparator]; NSArray *sortedValues = [self objectsForKeys:sortedKeys notFoundMarker:[NSNull null]]; return [PFXKeyValuePair pairsWithValues:sortedValues forKeys:sortedKeys]; } @end 

注意 :在上面, PFXpfx是占位符。 您应该用适合您的项目的前缀replace它们。

然后我们可以使用这个类别方法来填充我们的UITableView 。 假设我们有一个财产

 @property (nonatomic) NSDictionary *events; 

让我们假设表格视图只有一个部分显示这些事件。

然后我们可以在我们的UITableViewController子类中实现–tableView:numberOfRowsInSection:

 – (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.events.count; } 

在我们的–tableView:numberOfRowsInSection:实现中–tableView:numberOfRowsInSection:我们可以在字典中确定相应的条目,如下所示:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //... NSArray *pairs = [self.events pfx_keyValuePairsSortedByKeyUsingComparator:^(id obj1, id obj2) { NSNumber *key1 = (NSNumber *)obj1; NSNumber *key2 = (NSNumber *)obj2; return [key1 compare:key2]; }]; NSUInteger index = [indexPath indexAtPosition:1]; PFXKeyValuePair *pair = pairs[index]; /* At this point, pair.value will be a dictionary as in your output above holding a value for the key @"Event" and a value for the key @"ID". */ //... } 

通过使pairs成为一个属性,只有在必要时才计算它(例如,在重新加载表的数据之前只计算pairs ,可以使这个速度更快。

注意 :使用这种方法,字典仍然不会被序列化到磁盘(当调用-writeToDisk:atomically: “按顺序”,你的输出将仍然看起来像你的问题一样。 但是,这并不重要:当数据在表格视图中显示给用户时,数据将按照您希望的顺序排列。

这是一个例子,得到Emplyee列表NSMutableArray并创buildNSMutableDictionary …….

 NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; for (NSString *word in emloyees) { NSString *firstLetter = [[word substringToIndex:1] uppercaseString]; letterList = [dict objectForKey:firstLetter]; if (!letterList) { letterList = [NSMutableArray array]; [dict setObject:letterList forKey:firstLetter]; } [letterList addObject:word];}NSLog(@"dic %@",dict);