Convert NSArray to NSDictionary

How to convert NSArray to NSDictionary using an int field of array objects as a key for NSDictionary ?

+41
objective-c iphone nsarray nsdictionary
Sep 12 '09 at 10:49
source share
6 answers
 - (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]; } 
+50
Sep 12 '09 at 11:02
source share
β€” -

Try this magic:

 NSDictionary *dict = [NSDictionary dictionaryWithObjects:records forKeys:[records valueForKey:@"intField"]]; 

FYI is possible thanks to this built-in function:

 @interface NSArray(NSKeyValueCoding) /* Return an array containing the results of invoking -valueForKey: on each of the receiver elements. The returned array will contain NSNull elements for each instance of -valueForKey: returning nil. */ - (id)valueForKey:(NSString *)key; 
+70
Oct 16 '13 at 22:20
source share

This adds a category extension to NSArray . Requires C99 mode (which is used by default these days, but just in case).

In the .h file somewhere that could be #import ed by everyone ..

 @interface NSArray (indexKeyedDictionaryExtension) - (NSDictionary *)indexKeyedDictionary @end 

In the .m .. file

 @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 

Usage example:

 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; } 
+9
Sep 12 '09 at 12:49
source share

This is an example of creating an NSMutableDictionary from a list of NSMutableArray employees:

  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); 
+2
Oct 29 '15 at 7:04
source share
 - (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; } 
+1
Mar 15 '14 at 21:42
source share

I created a simple library called Linq to ObjectiveC , which is a set of methods that make this problem much easier. In your case, you need the Linq-to-ObjectiveC toDictionary method , where your 'int' field is set using the key selector:

 NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) { return [item intField]; }]; 

This returns a dictionary in which keys are specified by intField in the source array.

-2
Mar 06 '13 at 8:36
source share



All Articles