0

I have an array of NSColors, and an array of CGFloats signifying gradient stops. I can't figure out how to use these arrays to initialize a NSGradient.

I tried making these into an array of (NSColor, CGFloat)s, but NSGradient(colorsAndLocations: won't take it, since it expects varargs:

The code <code>let gradient = NSGradient(colorsAndLocations: colorsAndLocations)</code> yielding the error <code>Cannot convert value of type '[(NSColor, CGFloat)]' to expected argument type '(NSColor, CGFloat)'</code>

And NSGradient(colors:, atLocations:, colorSpace:) expects a UnsafePointer which I have no idea how to properly handle in Swift, if there is even such a way.

Ky -
  • 30,724
  • 51
  • 192
  • 308

1 Answers1

1

I assume you know this usage:

let cAndL: [(NSColor, CGFloat)] = [(NSColor.redColor(), 0.0), (NSColor.greenColor(), 1.0)]
let gradient = NSGradient(colorsAndLocations: cAndL[0], cAndL[1])

Unfortunately, Swift does not provide us a way to give Arrays to variadic functions.


And the second portion. If some API claims UnsafePointer<T> as an array, you can create a Swift Array of T, and pass it directly to the API.

let colors = [NSColor.redColor(), NSColor.greenColor()]
let locations: [CGFloat] = [0.0, 1.0]
let gradient2 = NSGradient(colors: colors, atLocations: locations, colorSpace: NSColorSpace.genericRGBColorSpace())

If you want to utilize an Array of (NSColor, CGFloat), you can write something like this:

let gradient3 = NSGradient(colors: cAndL.map{$0.0}, atLocations: cAndL.map{$0.1}, colorSpace: NSColorSpace.genericRGBColorSpace()) 
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • _"you can create a Swift Array of T, and pass it directly to the API"_ Is this guaranteed to be safe? – Ky - Aug 03 '16 at 17:44
  • 1
    @BenLeggiero, surely. See the Constant Pointers part of [this document](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID23). – OOPer Aug 03 '16 at 17:48