【发布时间】:2010-11-27 17:49:13
【问题描述】:
如何使用数组对象的 int 字段作为 NSDictionary 的键,将 NSArray 转换为 NSDictionary?
【问题讨论】:
标签: iphone objective-c nsarray nsdictionary
如何使用数组对象的 int 字段作为 NSDictionary 的键,将 NSArray 转换为 NSDictionary?
【问题讨论】:
标签: iphone objective-c nsarray nsdictionary
试试这个魔法:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:records
forKeys:[records valueForKey:@"intField"]];
仅供参考,因为有这个内置功能,这是可能的:
@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;
【讨论】:
这是从员工列表NSMutableArray 创建NSMutableDictionary 的示例:
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);
【讨论】:
- (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];
}
【讨论】:
- (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;
}
【讨论】:
我创建了一个简单的库,名为Linq to ObjectiveC,它是一个方法的集合,可以让这类问题更容易解决。在您的情况下,您需要 Linq-to-ObjectiveC toDictionary 方法,其中您的“int”字段是通过键选择器指定的:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
return [item intField];
}];
这将返回一个字典,其中键由源数组中的intField 给出。
【讨论】:
这会为NSArray 添加一个类别扩展。需要C99 模式(这是目前的默认模式,但以防万一)。
在.h 文件中的某个地方,所有人都可以#imported..
@interface NSArray (indexKeyedDictionaryExtension)
- (NSDictionary *)indexKeyedDictionary
@end
在.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
使用示例:
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two", NULL];
NSDictionary *dictionary = [array indexKeyedDictionary];
NSLog(@"dictionary: %@", dictionary);
输出:
2009-09-12 08:41:53.128 test[66757:903] dictionary: {
0 = zero;
1 = one;
2 = two;
}
【讨论】: