Swift相当于Array.componentsJoinedByString?
发布时间:2020-12-14 06:09:10 所属栏目:百科 来源:网络整理
导读:在Objective-C中,我们可以调用componentsJoinedByString生成一个字符串,数组的每个元素都由提供的字符串分隔。虽然Swift在String上有一个componentsSeparatedByString方法,但是在Array上并没有出现这种情况: 'ArrayString' does not have a member named
在Objective-C中,我们可以调用componentsJoinedByString生成一个字符串,数组的每个元素都由提供的字符串分隔。虽然Swift在String上有一个componentsSeparatedByString方法,但是在Array上并没有出现这种情况:
'Array<String>' does not have a member named 'componentsJoinedByString' Swift中componentsSeparatedByString的逆是什么?
Swift 3.0:
类似于Swift 2.0,但是API重命名已经将joinWithSeparator重命名为join(separator :)。 let joinedString = ["1","2","3","4","5"].joined(separator: ",") // joinedString: String = "1,2,3,4,5" 有关详细信息,请参阅Sequence.join(separator:)。 Swift 2.0: 您可以使用SequenceType上的joinWithSeparator方法将字符串数组与字符串分隔符联接。 let joinedString = ["1","5"].joinWithSeparator(",5" 有关详细信息,请参阅SequenceType.joinWithSeparator(_:)。 Swift 1.0: 您可以使用String上的连接标准库函数将字符串数组与字符串连接。 let joinedString = ",".join(["1","5"]) // joinedString: String = "1,5" 或者如果你愿意,你可以使用全局标准库函数: let joinedString = join(",",["1",5" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |