Reputation: 1951
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?
Upvotes: 3
Views: 21939
Reputation: 544
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. :)
Upvotes: 1
Reputation: 33
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);
Upvotes: 1
Reputation: 52237
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
)
Upvotes: 7
Reputation: 274
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];
Upvotes: 4
Reputation: 696
No, guys.... the problem is that you are stepping on the KeyValue Mechanism in cocoa.
KeyValueCoding specifies that the @count symbol can be used in a keyPath....
myArray.@count
SOOOOOO.... just switch to the ObjectForKey and your ok!
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"theValue", @"@name", nil];
id kvoReturnedObject = [myDictionary valueForKey:@"@name"]; //WON'T WORK, the @ symbol is special in the valueForKey
id dictionaryReturnedObject = [myDictionary objectForKey:@"@name"];
NSLog(@"object = %@", dictionaryReturnedObject);
Upvotes: 0
Reputation: 2261
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.
Upvotes: 3