views:

954

answers:

3

Why is the following code (in cocoa) not working?

NSString *extension = [fileName pathExtension];
NSString *wantedExtension = @"mp3";
if(extension == wantedExtension){
//work
}

in Xcode this just runs without warnings or errors but doesn't do what I think it SHOULD do.

+20  A: 

Shouldn't that be

if ([extension isEqualToString:wantedExtension]) {
...
}

"==" compares the pointers. isEqual: and isEqualToString: compare the strings, although isEqualToString is better if you know both extension and wantedExtension are NSString (which you do in this case).

Actually, if you're an old C++ and Java programmer like me, you might be happier putting the one that is known not to be null, "wantedextension", first. In Objective C that is not necessary because "sending a message" (ie calling a method) to a nil returns 0 or false.

if ([wantedExtension isEqualToString:extension]) {
   ...
}
Paul Tomblin
I see Paul has been studying Objective-C.
Kristopher Johnson
Yup, that's my first ObjC answer. Thanks for the code, by the way, it's very helpful.
Paul Tomblin
Given that isEqualToString returns a BOOL, and messaging nil will return 0, why is the second form safer?
Barry Wark
I don't think there's any real difference. You should get the same result if either is nil.
Martin Pilkington
Correct me if I'm wrong, but I thought the "message on a null returns 0 or false" was a new feature in 2.0?
Paul Tomblin
@Paul, I write Objective-C 1.0 code and frequently rely on sending message to nil returning 0. In fact, from memory, it's only safe to send messages to nil if the return value is declared as a 32-bit integer or pointer (for 32-bit machines). Floating points and 64-bit integers cause issues.
dreamlax
Structs are also unsafe if they are larger than the register size
rpetrich
Sorry, the difference I was thinking of was the difference between Mac OS X 10.5 and previous.
Paul Tomblin
+8  A: 

Paul's answer is technically correct, but as stated in the NSString documentation, "When you know both objects are strings, this method [isEqualToString:] is a faster way to check equality than isEqual:." Thus, for your example code, the correct test is

if([extension isEqualToString:wantedExtension]) {
    ...
}

If extension is nil, the result will be false, even if wantedExtension is non-nil, since messaging nil in Objective-C returns 0 for BOOL return-valued functions.

Barry Wark
I've edited my answer to use isEqualToString. Thanks.
Paul Tomblin
No worries. I have to look up the isEqualToX docs every time I use them!
Barry Wark
+2  A: 

Remember that in Objective-C there is no operator overloading. What the == is doing in this case is a perfectly legal and well-used usage, comparing two pointers. You have two pointers that will always point to two different objects, so the == operator will always be false.

dreamlax
Two NSStrings set to identical text point to the same object, masking the unsuitability of using == for comparisons. Fooled me into trusting it to match a literal and a returned string. Doh.
willc2