在Swift中“join”函数的目的
join()在数组中的作用是什么? 什么目的? 在其他语言中,它用于将数组的元素连接到string中。 例如,
Ruby Array.join
我已经问了一些关于在Swift数组join ()的问题joinEXC_BAD_ACCESS
这是一个有用的string示例:
Swift 3.0
let joiner = ":" let elements = ["one", "two", "three"] let joinedStrings = elements.joined(separator: joiner) print("joinedStrings: \(joinedStrings)")
输出:
joinedStrings:一:二:三
Swift 2.0
var joiner = ":" var elements = ["one", "two", "three"] var joinedStrings = elements.joinWithSeparator(joiner) print("joinedStrings: \(joinedStrings)")
输出:
joinedStrings:一:二:三
Swift 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);
输出:
joinedStrings:一:二:三