views:

1987

answers:

4

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!

A: 

Relevant discussion here.

Dimitri Tcaciuc
+27  A: 
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.

Jason Coco
@Adam - thanks for the link! I was feeling lazy today :)
Jason Coco
+6  A: 

The method you are looking for is componentsJoinedByString.

NSArray  a = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString b = [a componentsJoinedByString:@","];
NSLog(b); // Will output 1,2,3
Rémy BOURGOIN
+2  A: 

NSArray class reference:

NSArray *pathArray = [NSArray arrayWithObjects:@"here",
    @"be", @"dragons", nil];
NSLog(@"%@",
    [pathArray componentsJoinedByString:@" "]);
Georg