Re: Stupid objective-c question
Re: Stupid objective-c question
- Subject: Re: Stupid objective-c question
- From: Slipp Douglas Thompson <email@hidden>
- Date: Wed, 21 Sep 2016 19:37:06 -0500
> 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?
Yes that's why it works in your test, but as Graham Cox said, you “are taking advantage of undocumented implementation details”. This behavior is not a standard at all.
> So what would be the proper way to do it? Should I just define my own string constant?
As others have suggested, always use isEqual: or isEqualToString: when doing NSString comparisons, never ==. The Obj-C implementation does a pointer comparison first, so isEqual:/isEqualToString: do not incur a significant performance cost in this situation.
— Slipp
> On Sep 21, 2016, at 7:05 PM, Gabriel Zachmann <email@hidden> wrote:
>
>
>>>
>>> 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
_______________________________________________
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