This bridged cast may not work, as hatfinch describes in his answer here, because the CGColorRef returned from -CGColor
may not hang around after your last reference to the UIColor that generates it. I thought this was a bug, based on the discussion in this Apple developer forum thread, but it was a misreading of how to manage the lifetime of these CGColorRefs.
One way that this will work is to use the built-in bridging provided by the -CGColor
method on UIColor. Rather than saving out your CGColor to a temporary variable as you do above, you should be able to use something like the following:
NSArray *colors = [NSArray arrayWithObjects:(id)[color1 CGColor],
(id)[color2 CGColor], nil];
with color1
and color2
being UIColor instances.
The bridging is taken care of for you by the -CGColor
method, according to the "The Compiler Handles CF Objects Returned From Cocoa Methods" section of the Transitioning to ARC Release Notes. The documentation is currently missing the cast to id that I have above, which is required to get this to compile.
I've tested this, and it seems to work in my case, matching what Ben reports in the above-linked Developer Forums thread.
In addition to the above, you can explicitly retain and release the CGColorRefs returned from the -CGColor
method and bridge them across in your NSArray, again as hatfinch shows here.
__bridge
,__bridge_retained
or__bridge_transfer
– Martin UllrichCGColorRef startColor = xColor.CGColor;
where xColor is of type UIColor in the parent classes. Should I retain it? – Gytis