Re: Stupid objective-c question
Re: Stupid objective-c question
- Subject: Re: Stupid objective-c question
- From: Gabriel Zachmann <email@hidden>
- Date: Thu, 22 Sep 2016 02:05:02 +0200
>>
>> how can the compiler know that '==' in this case is a NSString comparison?
>
> It can’t because it isn't. What’s being compared are raw pointers. The string value is irrelevant.
Let me try to paraphrase, in order to check whether I am understanding correctly.
Whenever I have two string literals @"XYZ" at different places in the same compilation unit,
and the XYZ are identical, then the compiler (or the Objective-C standard) make sure that
the pointers to those literals are identical?
In other words, the compiler unifies the two occurrences of the two literals, thus effectively storing only one literal?
> So, this technique is generally Not A Good Idea™.
If my understanding is correct, then I wholeheartedly agree.
That brings me to another question. I've got this piece of code from an example on MLMediaLibrary.
This is how I start the whole thing:
[mediaLibrary_ addObserver: self
forKeyPath: @"mediaSources"
options: 0
context: (__bridge void *) @"mediaLibraryLoaded"];
And this is the beginning of the corresponding KVO:
- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object
change: (NSDictionary *) change context: (void *) context
{
MLMediaSource * mediaSource = [mediaLibrary_.mediaSources objectForKey: MLMediaSourcePhotosIdentifier];
if ( context == (__bridge void *) @"mediaLibraryLoaded" )
{
So what would be the proper way to do it? Should I just define my own string constant?
Best regards,
Gabriel.
_______________________________________________
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