Re: minor ARC casting question
Re: minor ARC casting question
- Subject: Re: minor ARC casting question
- From: Matt Neuburg <email@hidden>
- Date: Tue, 01 Nov 2011 17:06:27 -0700
On Sun, 30 Oct 2011 15:55:39 +0000, Igor Mozolevsky <email@hidden> said:
>On 30 October 2011 15:15, Matt Neuburg <email@hidden> wrote:
>> In ARC, this is legal:
>>
>> self.view.layer.contents = (id)[[UIImage imageNamed:@"boat.gif"] CGImage];
>>
>> And this is legal:
>>
>> id ref = (id)[[UIImage imageNamed:@"boat.gif"] CGImage];
>> self.view.layer.contents = ref;
>>
>> But this is not:
>>
>> CGImageRef ref = [[UIImage imageNamed:@"boat.gif"] CGImage];
>> self.view.layer.contents = (id)ref; // compilation fails
>>
>> In the last case, I have to change id to __bridge id. My question is: What's the difference in the cases? In all situations I'm casting a CGImageRef to an id, so why does ARC permit this in the first cases but not in the last? Is it because UIImage's CGImage method is a method, and this fact somehow gives ARC further info? Thx - m.
>
>The compiler knows how to handle CF objects returned from Cocoa
>methods, but doesn't know how to handle stuff created by yourself. I
>thought that was fairly obvious from and explicit in the Transitioning
>Notes?
>
It's no use referring me to the Transitioning Notes. The problem is in part that they are wrong. This makes it hard to know what to do. Presumably we're talking about this?
http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html
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.") 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.
--
matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
A fool + a tool + an autorelease pool = cool!
Programming iOS 4!
http://www.apeth.net/matt/default.html#iosbook_______________________________________________
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