Re: Strange cast for CFURLRef

Forgot to copy list.

On Tue, Mar 31, 2009 at 1:12 PM, Jim Correia <jim.correia@xxxxxxxxx> wrote:
> On Mar 31, 2009, at 1:32 PM, Patrick Burleson wrote:
>> I ran into something I don't quite understand and was hoping to get
>> some enlightenment. There's a method defined on AudioPlayer that goes
>> like this:
>> - (id) initWithURL: (CFURLRef) fileURL;
> [...]
>> The question I have is on the line:
>> AudioPlayer *thePlayer = [[AudioPlayer alloc] initWithURL: (NSURL
>> *)fileURL];
> +alloc returns an untyped (`id`) object.
> The compiler must consider all -initWithURL: methods that it has seen
> declared. Since the frameworks provide -initWithURL:(NSURL *)url (and the
> compiler doesn't know about the toll free bridging between NSURL and CFURL),
> the warning is generated.

I see with alloc returning (id) that it'd have to consider all the
initWithURL's it knows about. The exact warning is:

warning: passing argument 1 of 'initWithURL:' from incompatible pointer type

If the compiler is considering all initWithURLs and only fails when
it's not exactly NSURL *, then that would imply to me all other
initWithURLs take NSURL *? Or is it ignoring that the AudioPlayer.h
declares a version that takes CFURLRef?

> You can cast the argument, as you have done above. You can also cast the
> result of +alloc:
>        AudioPlayer *thePlayer = [(AudioPlayer *)[AudioPlayer alloc]
> initWithURL: fileURL];
> A better solution, though, is to avoid being in this situation in the first

t; place.
>        -initWithFoo:(Foo *)foo
>        -initWithFoo:(NSInteger)foo
> Avoid designing classes whose initializers which use identical names for
> different types.

Thanks for the advice. I should file a Radar on this sample code to
change it. Maybe to initWithCFURL. Or to make the change where all the
classes hold it as NSURL * until needed by the C APIs and the object
is cast to CFURLRef as needed.


Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to maillists@xxxxxxxxx