Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGGradient issues with swift 3 [duplicate]

Just in the process of converting my code from swift 2.3 to 3. In the old code I created a gradient through the following code:

        let colours:CFArrayRef = [tColour.CGColor, bColour.CGColor]
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let gradient = CGGradientCreateWithColors(colorSpace, colours, nil)

When Xcode 8 converted the code it changed it to the following:

    let colours:CFArray = [tColour.cgColor, bColour.cgColor]
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let gradient = CGGradient(colorsSpace: colorSpace, colors: colours, locations: nil)

However the code produces the following error:

Contextual type 'CFArray' cannot be used with array literal

Can anybody suggest how to convert the code properly.
With thanks
Reza

like image 907
reza23 Avatar asked Sep 15 '16 15:09

reza23


1 Answers

Cast the type

let colours = [tColour.cgColor, bColour.cgColor] as CFArray
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradient(colorsSpace: colorSpace, colors: colours , locations: nil)
like image 186
vadian Avatar answered Oct 14 '22 07:10

vadian