Re: Why NSClassFromString(@"CADisplayLink")?
Re: Why NSClassFromString(@"CADisplayLink")?
- Subject: Re: Why NSClassFromString(@"CADisplayLink")?
- From: Wim Lewis <email@hidden>
- Date: Tue, 10 May 2011 10:13:59 -0700
On 10 May 2011, at 3:34 AM, Brian Bruinewoud wrote:
> Just curious, why does this work (compiles and runs):
>
> displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: tapped selector:@selector(respond:)];
>
> But this doesn't link because the CADisplayLink class is missing:
>
> displayLink = [CADisplayLink displayLinkWithTarget: tapped selector:@selector(respond:)];
Because the second one causes the linker to find the class in the frameworks you're actually linking against, but the first one searches the process at runtime for a class by that name. Unless you're prepared for NSClassFromString() to return Nil, you should use the second form (and supply the appropriate framework to the linker so that it can resolve the class reference to the specific class in that framework).
The first form works because something else in the process happens to have loaded something that refers to CADisplayLink, but there's no guarantee that that will be true.
_______________________________________________
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