Bill Bill - 8 months ago 43
Swift Question

'Ambiguous reference' when using joinWithSeparator to intersperse into array

I'm trying to use joinWithSeparator to insert a separator element between the elements of an array. Based on the documentation, I should be able to do:

[1, 2, 3].joinWithSeparator([0])

to get:

[1, 0, 2, 0, 3]

Instead, I get:

repl.swift:3:11: error: type of expression is ambiguous without more context
[1, 2, 3].joinWithSeparator([0])

How can I do this?


joinWithSeparator does not work like this. The input should be a sequence of sequence i.e.

[[1], [2], [3]].joinWithSeparator([0])
// a lazy sequence that would give `[1, 0, 2, 0, 3]`.

You could also intersperse by flatMap and then drop the last separator:

[1, 2, 3].flatMap { [$0, 0] }.dropLast()