Re: Stupid objective-c question
Re: Stupid objective-c question
- Subject: Re: Stupid objective-c question
- From: Steve Mills <email@hidden>
- Date: Wed, 21 Sep 2016 18:52:50 -0500
> On Sep 21, 2016, at 18:44, Gabriel Zachmann <email@hidden> wrote:
>
> I've got a stupid, curious question regarding a code snippet that I have found on the net (I tried it, it works).
>
> Here is the code snippet:
>
> - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object
> change: (NSDictionary *) change context: (void *) context
> {
> if ( context == (__bridge void *) @"mediaLibraryLoaded" )
> {
> // ...
>
>
> My question is: how can the compiler know that '==' in this case is a NSString comparison?
> Or is some other magic going on here? if so, which?
> Does the compiler know it should perform some kind of dynamic method dispatch?
My guess, without seeing the code that set up the observer, is that it was also set up with @"mediaLibraryLoaded", and the compiler collects and reuses string constants, so the address is the same. I'd guess that if you ensure that the string is a unique variable, it won't work.
NSString* s = [NSString stringWithFormat:@"%@%@%@", @"media", @"Library", @"Loaded"];
if(context == (__bridge void*)s)
Steve via iPad
_______________________________________________
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