在新的Swift 2样式中,join必须由joinWithSeparator替换.但是我得到了错误消息,发现了这个模糊的引用:
var distribCharactersInt = [Int](count:lastIndex + 1, repeatedValue:0)
...
let DistributionCharacterString = distribCharactersInt.joinWithSeparator(",")
Run Code Online (Sandbox Code Playgroud)
我忘记了什么?
有两种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>
}
Run Code Online (Sandbox Code Playgroud)
另一个采用一系列字符串(和一个字符串作为分隔符):
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
}
Run Code Online (Sandbox Code Playgroud)
你可能想要使用第二种方法,但是你必须将数字转换为字符串:
let distribCharactersInt = [Int](count:5, repeatedValue:0)
let distributionCharacterString = distribCharactersInt.map(String.init).joinWithSeparator(",")
print(distributionCharacterString) // 0,0,0,0,0
Run Code Online (Sandbox Code Playgroud)