Peter71
Peter71

Reputation: 2310

join to joinWithSeparator for int array?

In new Swift 2 style join has to be replaced by joinWithSeparator. But I get the error mesage, that ambiguous references were found for this :

var distribCharactersInt = [Int](count:lastIndex + 1, repeatedValue:0)
...                        
let DistributionCharacterString = distribCharactersInt.joinWithSeparator(",")

What did I forget?

Upvotes: 4

Views: 6283

Answers (1)

Martin R
Martin R

Reputation: 540105

There are two joinWithSeparator() methods. One takes a sequence of sequences:

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>
}

and the other takes a sequence of strings (and a string as separator):

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
}

You'll probably want to use the second method, but then you have to convert the numbers to strings:

let distribCharactersInt = [Int](count:5, repeatedValue:0)
let distributionCharacterString = distribCharactersInt.map(String.init).joinWithSeparator(",")
print(distributionCharacterString) // 0,0,0,0,0

Upvotes: 9

Related Questions