在 Objective-C 中加入数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/845622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Join an Array in Objective-C
提问by Codebeef
I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?
我正在寻找一种将 NSMutableArray 转换为字符串的方法。有什么可以与这个 Ruby 数组方法相提并论的吗?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Cheers!
干杯!
回答by Jason Coco
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];
componentsJoinedByString:will join the components in the array by the specified string and return a string representation of the array.
componentsJoinedByString:将通过指定的字符串连接数组中的组件并返回数组的字符串表示形式。
回答by Rémy
The method you are looking for is 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
回答by Georg Sch?lly
NSArray *pathArray = [NSArray arrayWithObjects:@"here",
@"be", @"dragons", nil];
NSLog(@"%@",
[pathArray componentsJoinedByString:@" "]);

