0

Before upgrading to Swift 3 and Xcode 8, my StyleKit project worked beautifully. Here is my code for some of the colours:

   //// Color Declarations
    let purplebliss1 = UIColor(red: 0.212, green: 0.000, blue: 0.200, alpha: 1.000)
    let purplebliss2 = UIColor(red: 0.043, green: 0.529, blue: 0.576, alpha: 1.000)

//// Color Declarations
    let venice1 = UIColor(red: 0.522, green: 0.847, blue: 0.808, alpha: 1.000)
    let venice2 = UIColor(red: 0.031, green: 0.314, blue: 0.471, alpha: 1.000)
    let aqua1 = UIColor(red: 0.149, green: 0.816, blue: 0.808, alpha: 1.000)
    let aqua2 = UIColor(red: 0.102, green: 0.161, blue: 0.502, alpha: 1.000)


    //// Color Declarations
    let mantle1 = UIColor(red: 0.141, green: 0.776, blue: 0.863, alpha: 1.000)
    let mantle2 = UIColor(red: 0.318, green: 0.290, blue: 0.616, alpha: 1.000)

However, it is now saying that these lines of code contain an error:

//// Gradient Declarations
let purpleBliss = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [purplebliss2.CGColor, purplebliss2.blendedColorWithFraction(0.5, ofColor: purplebliss1).CGColor, purplebliss1.CGColor], [0, 0.27, 1])!

//// Gradient Declarations
    let mantle = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [mantle1.CGColor, mantle2.CGColor], [0, 1])!

 //// Gradient Declarations
    let veniceBeach = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [venice2.CGColor, venice1.CGColor], [0, 1])!
    let aqua = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [aqua1.CGColor, aqua2.CGColor], [0, 1])!

The error its consistently saying is:

contexttual type 'CFArray' cannot be used with array literal

I have no idea how to fix this. Any advice?

Thanks everyone :)

2 Answers2

1

(Updated)Try creating an CFArray like this:

let purpleBliss = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
                                  colors: [purplebliss2.cgColor, purplebliss2.blendedColorWithFraction(0.5, ofColor: purplebliss1).cgColor, purplebliss1.cgColor] as CFArray,
                                 locations: [0, 0.27, 1])!

The other gradients would be like this:

let mantle = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
                         colors: [mantle1.cgColor, mantle2.cgColor] as CFArray,
                         locations: [0, 1])!

let veniceBeach = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), 
                             colors: [venice2.cgColor, venice1.cgColor] as CFArray, 
                             locations: [0, 1])!

let aqua = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(),
                      colors:[aqua1.cgColor, aqua2.cgColor] as CFArray, 
                      locations: [0, 1])!

To sum up, the changes to be made when updating to swift 3 are:

  1. CGGradientCreateWithColors is now CGGradient.
  2. You have to put all the labels of the arguments in your call (colorsSpace, colors and locations).
  3. .CGColor is now .cgColor.
  4. In Swift 3 implicit casting to bridged types has been removed, so you have to cast the CFArray yourself.
ACBM
  • 657
  • 1
  • 10
  • 26
  • Thanks! What about the other gradients- how would I do those? – Aditya Rudrapatna Sep 28 '16 at 13:27
  • The same way, you just have to change `CGGradientCreateWithColors` to `CGGradient`, `.CGColor` to `.cgColor`, and the array cast it as `CFArray`. I'm editing my answer to include the other gradients :) – ACBM Sep 28 '16 at 15:23
  • oh wait- just one problem. for the new purple bliss code you gave me, it returns the same error as before – Aditya Rudrapatna Sep 28 '16 at 16:54
  • That's weird, is this working for you: `let purpleBliss = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: [purplebliss2.cgColor, purplebliss2.cgColor, purplebliss1.cgColor] as CFArray, locations: [0, 0.27, 1])!`? If it is, I think you should check this answer: http://stackoverflow.com/questions/36363989/blendedcolorwithfractionofcolor-in-ios-swift-and-objc – ACBM Sep 28 '16 at 20:41
0

It is worth mentioning, that PaintCode 3 fully supports export to Swift 3.

— PaintCode Support

Tricertops
  • 8,492
  • 1
  • 39
  • 41