objective-c 将 NSArray 转换为 NSDictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1414852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert NSArray to NSDictionary
提问by johnl
How can I convert an NSArrayto an NSDictionary, using an int field of the array's objects as key for the NSDictionary?
如何使用数组对象的 int 字段作为 的键将 an 转换NSArray为 an ?NSDictionaryNSDictionary
回答by malhal
Try this magic:
试试这个魔法:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:records
forKeys:[records valueForKey:@"intField"]];
FYI this is possible because of this built-in feature:
仅供参考,由于此内置功能,这是可能的:
@interface NSArray(NSKeyValueCoding)
/* Return an array containing the results of invoking -valueForKey:
on each of the receiver's elements. The returned array will contain
NSNull elements for each instance of -valueForKey: returning nil.
*/
- (id)valueForKey:(NSString *)key;
回答by Alex Reynolds
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
id objectInstance;
NSUInteger indexKey = 0U;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
for (objectInstance in array)
[mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];
return (NSDictionary *)[mutableDictionary autorelease];
}
回答by johne
This adds a category extension to NSArray. Needs C99mode (which is the default these days, but just in case).
这将添加一个类别扩展到NSArray. 需要C99模式(这是这些天的默认模式,但以防万一)。
In a .hfile somewhere that can be #imported by all..
在一个.h可以被#import所有人编辑的文件中..
@interface NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
@end
In a .mfile..
在一个.m文件..
@implementation NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
{
NSUInteger arrayCount = [self count];
id arrayObjects[arrayCount], objectKeys[arrayCount];
[self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)];
for(NSUInteger index = 0UL; index < arrayCount; index++) { objectKeys[index] = [NSNumber numberWithUnsignedInteger:index]; }
return([NSDictionary dictionaryWithObjects:arrayObjects forKeys:objectKeys count:arrayCount]);
}
@end
Example use:
使用示例:
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two", NULL];
NSDictionary *dictionary = [array indexKeyedDictionary];
NSLog(@"dictionary: %@", dictionary);
Outputs:
输出:
2009-09-12 08:41:53.128 test[66757:903] dictionary: {
0 = zero;
1 = one;
2 = two;
}
回答by AcornHacks
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
[array enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop){
NSNumber *index = [NSNumber numberWithInteger:idx];
[mutableDictionary setObject:obj forKey:index];
}];
NSDictionary *result = [NSDictionary.alloc initWithDictionary:mutableDictionary];
return result;
}
回答by damith
This is an example of creating a NSMutableDictionaryfrom the employee list NSMutableArray:
这是NSMutableDictionary从员工列表创建 a 的示例NSMutableArray:
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);
回答by ColinE
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC toDictionarymethod, where your 'int' field is specified via a key selector:
我创建了一个简单的库,称为Linq to ObjectiveC,它是一组方法,可以使此类问题更容易解决。在您的情况下,您需要Linq-to-ObjectiveC toDictionary方法,其中您的 'int' 字段是通过键选择器指定的:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
return [item intField];
}];
This returns a dictionary where the keys are given by the intFieldin the source array.
这将返回一个字典,其中的键由intField源数组中的 给出。

