有两种joinWithSeparator()
方法。一个需要序列的 序列:
extension SequenceType where Generator.Element : SequenceType {
/// Returns a view, whose elements are the result of interposing a given
/// `separator` between the elements of the sequence `self`.
///
/// For example,
/// `[[1, 2, 3], [4, 5, 6], [7, 8, 9]].joinWithSeparator([-1, -2])`
/// yields `[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]`.
@warn_unused_result
public func joinWithSeparator<Separator : SequenceType where Separator.Generator.Element == Generator.Element.Generator.Element>(separator: Separator) -> JoinSequence<Self>
}
和其他需要串序列(和字符串作为分隔符):
extension SequenceType where Generator.Element == String {
/// Interpose the `separator` between elements of `self`, then concatenate
/// the result. For example:
///
/// ["foo", "bar", "baz"].joinWithSeparator("-|-") // "foo-|-bar-|-baz"
@warn_unused_result
public func joinWithSeparator(separator: String) -> String
}
你可能会想要使用第二种方法,但那么你有 将数字转换为字符串:
let distribCharactersInt = [Int](count:5, repeatedValue:0)
let distributionCharacterString = distribCharactersInt.map(String.init).joinWithSeparator(",")
print(distributionCharacterString) // 0,0,0,0,0
很好。有用!非常感谢! – Peter71