AJS AJS - 1 year ago 122
Swift Question

Swift ambiguous reference to member '-' in array joining

I'm running into some issues creating an array out of other arrays and slices of other arrays. All arrays referenced here are internal static let [UInt16] declared earlier. They are all contained in the same struct.

internal static let bigarray = [UInt16]([0, array1, array2, array3, array4, array5, [UInt16](array6.prefix(upTo: array6.count-8))].joined())

If the stuff involving array6 is removed, everything is hunky-dory. But as soon as I add the array6 slice, Swift complains that the - (minus) operator is ambiguous. How is this possible? Since array6.count and 8 are both Ints, shouldn't it just do simple integer subtraction and pass that as the upTo argument for prefix?

Answer Source

The error message is misleading. The real problem is that the first element 0 in the "array of arrays"

[0, array1, array2, ... ]

is not an array, but a number. Changing that to [0] should solve the problem. Note that the expression can be simplified slightly to

let bigarray = [[0], array1, array2, array3, array4, array5, Array(array6.dropLast(8))].joined()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download