Convert NSMutableArray to NSDictionary in order to use objectForKey?

21,037

Solution 1

There is a even shorter form then this proposed by Hubert

NSArray *allNames = [array valueForKey:@"name"];

valueForKey: on NSArray returns a new array by sending valueForKey:givenKey to all it elements.

From the docs:

valueForKey:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.

- (id)valueForKey:(NSString *)key

Parameters
key The key to retrieve.

Return Value
The value of the retrieved key.

Discussion
The returned array contains NSNull elements for each object that returns nil.

Example:

NSArray *array = @[@{ @"active": @NO,@"name": @"Alice"},
                   @{ @"active": @NO,@"name": @"Bob"}];

NSLog(@"%@\n%@", array, [array valueForKey:@"name"]);

result:

(
        {
        active = 0;
        name = Alice;
    },
        {
        active = 0;
        name = Bob;
    }
)
(
    Alice,
    Bob
)

Solution 2

If you want to convert NSMutableArray to corresponding NSDictionary, just simply use mutableCopy

NSMutableArray *phone_list; //your Array
NSDictionary *dictionary = [[NSDictionary alloc] init];
dictionary = [phone_list mutableCopy];

Solution 3

This is an Array of Dictionary objects, so to get the values you would:

[[myArray objectAtIndex:0]valueForKey:@"name"]; //Replace index with the index you want and/or the key.

Solution 4

yes you can

see this example:

NSDictionary *responseDictionary = [[request responseString] JSONValue];
NSMutableArray *dict = [responseDictionary objectForKey:@"data"];
NSDictionary *entry = [dict objectAtIndex:0];
NSString *num = [entry objectForKey:@"num"]; 
NSString *name = [entry objectForKey:@"name"];
NSString *score = [entry objectForKey:@"score"];

im sorry if i can't elaborate much because i am also working on something

but i hope that can help you. :)

Solution 5

This is example one of the exmple get the emplyee list NSMutableArray and create 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);
Share:
21,037
davis
Author by

davis

SOreadytohelp

Updated on July 09, 2022

Comments

  • davis
    davis almost 2 years

    I have an NSMutableArray that looks like this

     {
        "@active" = false;
        "@name" = NAME1;
     },
     {
        "@active" = false;
        "@name" = NAME2;
     }
    

    Is there a way to convert this to an NSDictionary and then use objectForKey to get an array of the name objects? How else can I get these objects?

  • NoobMe
    NoobMe over 12 years
    @Davis G. - i guess Hubert's answer is much simpler and easy to use.
  • davis
    davis over 12 years
    I'm assuming you meant valueForKey:@"@name", but I tried this and it throws NSUnknownKeyException. I made this array from an nsdictionary so it may not be set up as an "Array of Dictionary objects" I didn't think I had any other option. See here
  • davis
    davis over 12 years
    I figured it out and set it up as an NSDictionary instead of NSArray. Thanks
  • vikingosegundo
    vikingosegundo over 12 years
    There is even a shorter form, see my answer.
  • NoobMe
    NoobMe over 12 years
    @vikingosegundo - sir hi.. can you help me out in a cocos2d i have problem i had question ban thats why i cant post questions i really need help can you please?
  • vikingosegundo
    vikingosegundo over 12 years
    What is a question ban? and no, I have no experience with cocos2d.
  • Stornu2
    Stornu2 over 8 years
    best solution by far
  • pkamb
    pkamb over 8 years
    This... doesn't work. Your dictionary variable here will actually be an array.