Search code examples
swiftcgfloatunsafemutablepointer

'map' produces [T], not the expected contextual result type 'UnsafeMutablePointer<CGFloat>'


I am calling getLineDash of a UIBeizerPath with this:

path.getLineDash(pattern.map{CGFloat($0)}, ...)

Where pattern is a [Float] (And I must use [Float], not [CGFloat]) following Fast method to cast [Float] to [CGFloat]? to cast it to [CGFloat], but it is giving me

Cannot convert value of type '[CGFloat]' to expected argument type 'UnsafeMutablePointer<CGFloat>?'

Weirdly, performing

path.setLineDash(pattern.map{CGFloat($0)}, ...

Does not raise a compile error.

Following this question, I added as UnsafeMutablePointer<CGFloat> but it is still giving me the error.


Solution

  • I was a bit stupid. I didn't fully understand UnsafeMutablePointers.

    basically they are inout parameters, so they need the ampersand (&) before the function call. also, as it is inout, I must pass a variable of [CGFloat] to it.

    This code worked:

    var pat = pattern.map{CGFloat($0)}
    var cou = count
    var phas = phase.map{CGFloat($0)}
    path.getLineDash(&pat, count: &cou, phase: &phas)