How to convert NSArray into NSString in objective-c?
Asked
Active
Viewed 2.4k times
9
-
1Could you be more specific? An NSArray can contain instances of any Objective-C class. – Frank Shearar Mar 05 '10 at 13:02
-
1See: http://stackoverflow.com/questions/1828665/convert-nsarray-to-nsstring-objective-c – mfazekas Mar 05 '10 at 13:07
-
[Convert NSString separated by comma to NSArray](http://stackoverflow.com/a/8204215/194544) – beryllium Mar 26 '12 at 19:53
-
Well, if you're not too particular you can always use `description`. – Hot Licks Aug 02 '13 at 16:00
4 Answers
29
Bearing in mind that you don't say what's in the NSArray, you can do this:
NSArray *arr = [NSArray arrayWithObjects: @"foo", @"bar", @"baz"];
[arr componentsJoinedByString: @","]

Frank Shearar
- 17,012
- 8
- 67
- 94
-
Yes, I find that this is strongly assuming that you are having text. Or is it using description field instead? Most likely... But barely usable in that context. – nembleton Oct 01 '12 at 21:26
12
Aternatively to Frank's method, which works pretty well, you could also do
NSString *myArrayString = [array description];
The default implementation of description on NSArray will print out the contents in a neatly formatted fashion.

Jasarien
- 58,279
- 31
- 157
- 188
-
1one problem here is that myArrayString will have {} around the text...how to get rid of this? thanks! – TommyG Nov 04 '11 at 23:26
4
This is how I have converted my NSArray
to NSString
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:aArray options:kNilOptions error:&error];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Radim Köhler
- 122,561
- 47
- 239
- 335

TheGreen
- 41
- 2
-
This is the safest method I've found assuming you eventually want to get back to an array state. Thanks! – Albert Renshaw May 02 '17 at 01:21
1
Swift 3.0 latest updates
let string = array.componentsJoined(by: ",")
you can used any separator in function which you want to separate the array elements currently in above example is ",".

Sai Apps
- 3
- 1

Chavda jaydeep
- 332
- 3
- 9