数组 – Swift中数组“Join”函数的用途[closed]

前端之家收集整理的这篇文章主要介绍了数组 – Swift中数组“Join”函数的用途[closed]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在数组中使用join()是什么?什么目的?
在其他语言中,它用于将数组的元素连接成字符串。例如,
Ruby Array.join

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

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

Swift 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

原文链接:https://www.f2er.com/swift/320925.html

猜你在找的Swift相关文章