在Objective-C中join数组

我正在寻找一种将NSMutableArray转换为string的方法。 有什么与这个Ruby数组方法相提并论吗?

>> array1 = [1, 2, 3] >> array1.join(',') => "1,2,3" 

干杯!

 NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; NSString *joinedString = [array1 componentsJoinedByString:@","]; 

componentsJoinedByString:将通过指定的string连接数组中的组件,并返回数组的string表示forms。

你正在寻找的方法是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 

NSArray类参考 :

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