Reputation: 27072
I'm trying to convert NSArray
to NSString
which contains NSDictionary
, I can achieve this with iteration through array and append string with the last one but I think there should be some easy way to achieve this something like componentsJoinedByString:
Is there any?
A sample array I've is,
[o] {name="ABC", age = "19", package = "3.4"}
[1] {name="PQR", age = "33", package = "5.0"}
[2] {name="XYZ", age = "24", package = "2.2"}
What I'm doing right now is,
NSString *name = @"";
NSString *age = @"";
NSString *package = @"";
for(NSDictionary *p in array)
{
name = [name stringByAppendingFormat:@"%@,",[p valueForKey:@"name"]];
age = [age stringByAppendingFormat:@"%@,",[p valueForKey:@"age"]];
package = [package stringByAppendingFormat:@"%@,",[p valueForKey:@"package"]];
}
name = [name substringToIndex:[name length] - 1];
age = [age substringToIndex:[age length] - 1];
package = [package substringToIndex:[package length] - 1];
NSLog(@"%@",name);
NSLog(@"%@",age);
NSLog(@"%@",package);
Is there more easy way to get this?
Upvotes: 0
Views: 541
Reputation: 46563
Do it simply :
Assuming you have array
contains objects of NSDictionary.
NSString *name = [[array valueForKey:@"name"] componentsJoinedByString:@","];
NSString *age = [[array valueForKey:@"age"] componentsJoinedByString:@","];
NSString *package = [[array valueForKey:@"package"] componentsJoinedByString:@","];
NSLog(@"%@",name);
NSLog(@"%@",age);
NSLog(@"%@",package);
Upvotes: 2