开发者

How to convert NSArray into NSString

开发者 https://www.devze.com 2022-12-22 04:37 出处:网络
How to convert NSArray into NSString in objective-开发者_运维百科c?Bearing in mind that you don\'t say what\'s in the NSArray, you can do this:

How to convert NSArray into NSString in objective-开发者_运维百科c?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消