0

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.

Community
  • 1
  • 1
Papershine
  • 4,995
  • 2
  • 24
  • 48
  • See: http://stackoverflow.com/questions/39515173/how-to-use-unsafemutablepointer-in-swift-3 – shallowThought Apr 15 '17 at 14:15
  • The method `setLineDash` takes `UnsafePointer`, so you could use `map`. (Though, it is not an efficient way to call `map` at each invocation of `setLineDash`.) You **must** use `[CGFloat]` (or manually allocated region of `CGFloat`s) to call `getLineDash`. – OOPer Apr 15 '17 at 18:31

1 Answers1

2

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)
Papershine
  • 4,995
  • 2
  • 24
  • 48