Re: minor ARC casting question
Re: minor ARC casting question
- Subject: Re: minor ARC casting question
- From: David Duncan <email@hidden>
- Date: Tue, 1 Nov 2011 17:21:30 -0700
On Nov 1, 2011, at 5:06 PM, Matt Neuburg wrote:
> Well, the example code there says:
>
> CAGradientLayer *gradientLayer = (CAGradientLayer *)[self layer];
> gradientLayer.colors = [NSArray arrayWithObjects:[[UIColor darkGrayColor] CGColor],
> [[UIColor lightGrayColor] CGColor], nil];
>
> The trouble is that in actual fact **that code does not compile** under ARC. ("Implicit conversion of a non-Objective-C pointer type 'CGColorRef' (aka 'struct CGColor *') to 'id' is disallowed with ARC.")
This compiles for me under ARC:
NSArray *array = [NSArray arrayWithObject:[[UIColor greenColor] CGColor]];
Since +arrayWithObjects is variadric, the parameters past the first don't need any consideration, so the comparison to +arrayWithObject: should be identical.
> So your humble narrator is no wiser than before about how to silence the compiler while properly crossing the bridge. My solution, discovered purely experimentation, is make the conversion explicit; this *does* compile:
>
> CAGradientLayer *gradientLayer = [CAGradientLayer layer];
> gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor] CGColor],
> (id)[[UIColor lightGrayColor] CGColor], nil];
>
> And now we're back to my original question. Is that the right way? And why am I able to get away without saying __bridge id (or whatever), as I would have to do if I were generating my own CGColor and not returning the CGColor via a built-in method? m.
Exactly what you do depends on your needs. For general parameter passing a __bridge cast (ala (__bridge id)foo) works fine. If you need to to manage ownership, then CFBridgingRetain() or CFBridgingRelease() should be used.
--
David Duncan
_______________________________________________
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