Join an Array in Objective-C

Codebeef picture Codebeef · May 10, 2009 · Viewed 51.7k times · Source

I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?

>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"

Cheers!

Answer

Jason Coco picture Jason Coco · May 10, 2009
NSArray  *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];

componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.