在 Objective-C 中加入数组

我正在寻找一种将 NSMutableArray 转换为字符串的方法。这个 Ruby 数组方法有什么相似之处吗?

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

干杯!

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

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

您正在寻找的方法是 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:@" "]);