在Objective-C中加入一个数组

Cod*_*eef 128 objective-c nsmutablearray nsarray

我正在寻找一种将NSMutableArray转换为字符串的方法.有没有与这个Ruby数组方法相同的东西?

>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Run Code Online (Sandbox Code Playgroud)

干杯!

Jas*_*oco 272

NSArray  *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];
Run Code Online (Sandbox Code Playgroud)

componentsJoinedByString: 将通过指定的字符串连接数组中的组件并返回该数组的字符串表示形式.


Rém*_*émy 17

你正在寻找的方法是componentsJoinedByString.

NSArray  *a = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];//returns a pointer to NSArray
NSString *b = [a componentsJoinedByString:@","];//returns a pointer to NSString
NSLog(@"%@", b); // Will output 1,2,3
Run Code Online (Sandbox Code Playgroud)


Geo*_*lly 6

NSArray课程参考:

NSArray *pathArray = [NSArray arrayWithObjects:@"here",
    @"be", @"dragons", nil];
NSLog(@"%@",
    [pathArray componentsJoinedByString:@" "]);
Run Code Online (Sandbox Code Playgroud)