简体   繁体   中英

How to convert NSArray into NSString

如何在Objective-C中将NSArray转换为NSString?

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: @","]

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.

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];

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 ",".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM