How to convert NSArray into NSString

前端 未结 4 1008
北恋
北恋 2020-12-15 11:33

How to convert NSArray into NSString in objective-c?

相关标签:
4条回答
  • 2020-12-15 12:14

    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.

    0 讨论(0)
  • 2020-12-15 12:24

    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];
    
    0 讨论(0)
  • 2020-12-15 12:34

    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 讨论(0)
  • 2020-12-15 12:38

    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: @","]
    
    0 讨论(0)
提交回复
热议问题