Re: Swift 3: How to Create CFArray of CGColors?
Re: Swift 3: How to Create CFArray of CGColors?
- Subject: Re: Swift 3: How to Create CFArray of CGColors?
- From: Quincey Morris <email@hidden>
- Date: Tue, 22 Nov 2016 16:36:02 -0800
- Feedback-id: 167118m:167118agrif8a:167118s3bJ-uowYh:SMTPCORP
On Nov 22, 2016, at 16:17 , Charles Jenkins <email@hidden> wrote:
>
> I have this line of code:
>
> let gradient = CGGradient( colorsSpace: CGColorSpaceCreateDeviceRGB(),
> colors: [ clearWhite.cgColor, clearWhite.cgColor,
> clearWhite.blendedColorWithFraction(0.5, ofColor: white).cgColor,
> white.cgColor ], locations: [0, 0.57, 0.93, 1])
>
> The array of colors is no longer legal in Swift 3. I get an error
> message—“Contextual type CFArray cannot be used with an array
> literal”—which I think is saying I must pass a CFArray.
The immediate problem is that the error message is spurious. Method “blendedColorWithFraction(:ofColor:)” has been renamed in Swift 3 to “blendedColor(withFraction:of:)”, so your array cannot be constructed as the desired type "[GCColor]".
The secondary problem is that “blendedColor(withFraction:of:)” returns an optional, so you need a “!” operator after it.
The third problem is that you need “as CGArray”. There would have been a fix-it for this, if the first two problems had been fixed. The following code compiles in a (macOS) playground:
> import AppKit
>
> let clearWhite = NSColor.white // or whatever
> let white = NSColor.white // or whatever
>
> let colors = [ clearWhite.cgColor, clearWhite.cgColor,
> clearWhite.blended(withFraction: 0.5, of: white)!.cgColor,
> white.cgColor ]
>
> let gradient = CGGradient( colorsSpace: CGColorSpaceCreateDeviceRGB(),
> colors: colors as CFArray, locations: [0, 0.57, 0.93, 1])
I would expect the iOS version is the same, if that’s what you need.
For puzzling problems like this, I suggest “unrolling” the parameters like the above, specifying explicit type annotations if you’re not sure what types are being inferred.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden