Re: minor ARC casting question
Re: minor ARC casting question
- Subject: Re: minor ARC casting question
- From: Igor Mozolevsky <email@hidden>
- Date: Wed, 02 Nov 2011 11:31:49 +0000
On 2 November 2011 00:06, Matt Neuburg <email@hidden> wrote:
> 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?
Are you saying that because someone at Apple was being sloppy in
writing that sample code and, as it transpires, hasn't fixed that
mis-type yet, the document is invalidated as a whole?
You need to stop thinking what you are trying to do in your head and
start thinking how compiler does its automagic. Once you start using
your own types that are not managed, the compiler doesn't have a clue
what to do with that, so it waves a huge white flag---the compiler has
no clue what you intend to do with `ref' of type `CGImageRef'. What
bridging does is it tells the compiler to stop worrying because _you_
have taken over the management of the type. At least that's how I
understood it from the notes.
Cheers,
--
Igor
_______________________________________________
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