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: Charles Jenkins <email@hidden>
- Date: Wed, 23 Nov 2016 07:19:29 -0500
Thank you, Eric and Quincey! Both Eric’s resource and Quincey’s explanation
gave good ideas on how to tackle problems like this in the future, which
will probably help with the next stumbling block. Cheers!
On Tue, Nov 22, 2016 at 7:36 PM, Quincey Morris <
email@hidden> wrote:
> 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.
>
>
--
Charles
_______________________________________________
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