在数组中使用join()是什么?什么目的?
在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join
在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join
我问了一些关于join()的问题
Swift Array join EXC_BAD_ACCESS
这里是一个有用的字符串示例:
原文链接:https://www.f2er.com/swift/320925.htmlSwift 3.0
let joiner = ":" let elements = ["one","two","three"] let joinedStrings = elements.joined(separator: joiner) print("joinedStrings: \(joinedStrings)")
输出:
joinedStrings: one:two:three
Swift 2.0
var joiner = ":" var elements = ["one","three"] var joinedStrings = elements.joinWithSeparator(joiner) print("joinedStrings: \(joinedStrings)")
输出:
joinedStrings: one:two:three
Swift 1.2:
var joiner = ":" var elements = ["one","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);
输出:
joinedStrings: one:two:three