60

join()数组中有什么用?

在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join

我问了一些关于 Swift Array join EXC_BAD_ACCESS中的 join() 的问题

4

1 回答 1

160

这是一个有用的字符串示例:

斯威夫特 3.0

let joiner = ":"
let elements = ["one", "two", "three"]
let joinedStrings = elements.joined(separator: joiner)
print("joinedStrings: \(joinedStrings)")

输出:

加入字符串:一:二:三

斯威夫特 2.0

var joiner = ":"
var elements = ["one", "two", "three"]
var joinedStrings = elements.joinWithSeparator(joiner)
print("joinedStrings: \(joinedStrings)")

输出:

加入字符串:一:二:三

斯威夫特 1.2:

var joiner = ":"
var elements = ["one", "two", "three"]
var joinedStrings = joiner.join(elements)
println("joinedStrings: \(joinedStrings)")

Obj-C 中的相同内容进行比较:

NSString *joiner = @":";
NSArray *elements = @[@"one", @"two", @"three"];
NSString *joinedStrings = [elements componentsJoinedByString:joiner];
NSLog(@"joinedStrings: %@", joinedStrings);

输出:

加入字符串:一:二:三

于 2014-07-18T13:44:01.920 回答