How to convert NSArray into NSString

Deadly 提交于 2019-11-27 15:13:13

问题


How to convert NSArray into NSString in objective-c?


回答1:


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



回答2:


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.




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/2386834/how-to-convert-nsarray-into-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!