Convert NSMutableArray to NSDictionary in order to use objectForKey?
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 invokingvalueForKey:
usingkey
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 containsNSNull
elements for each object that returnsnil
.
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);
Comments
-
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 over 12 years@Davis G. - i guess Hubert's answer is much simpler and easy to use.
-
davis over 12 yearsI'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 over 12 yearsI figured it out and set it up as an NSDictionary instead of NSArray. Thanks
-
vikingosegundo over 12 yearsThere is even a shorter form, see my answer.
-
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 over 12 yearsWhat is a question ban? and no, I have no experience with cocos2d.
-
Stornu2 over 8 yearsbest solution by far
-
pkamb over 8 yearsThis... doesn't work. Your
dictionary
variable here will actually be an array.