Convert NSArray to NSString in Objective-C
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
One approach would be to iterate over the array, calling the description
message on each item:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", result);
Another approach would be to do something based on each item's class:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
if ([obj isKindOfClass:[NSNumber class]])
{
// append something
}
else
{
[result appendString:[obj description]];
}
}
NSLog(@"The concatenated string is %@", result);
If you want commas and other extraneous information, you can just do:
NSString * result = [array description];
I think Sanjay's answer was almost there but i used it this way
NSArray *myArray = [[NSArray alloc] initWithObjects:@"Hello",@"World", nil];
NSString *greeting = [myArray componentsJoinedByString:@" "];
NSLog(@"%@",greeting);
Output :
2015-01-25 08:47:14.830 StringTest[11639:394302] Hello World
As Sanjay had hinted - I used method componentsJoinedByString from NSArray that does joining and gives you back NSString
BTW NSString has reverse method componentsSeparatedByString that does the splitting and gives you NSArray back .