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.

Redwood
  • 66,744
  • 41
  • 126
  • 187
xaddict
  • 1,302
  • 3
  • 19
  • 38

3 Answers3

23

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
  • 179,021
  • 58
  • 319
  • 408
  • Yup, that's my first ObjC answer. Thanks for the code, by the way, it's very helpful. – Paul Tomblin Jan 27 '09 at 20:15
  • Given that isEqualToString returns a BOOL, and messaging nil will return 0, why is the second form safer? – Barry Wark Jan 27 '09 at 22:25
  • I don't think there's any real difference. You should get the same result if either is nil. – Martin Pilkington Jan 27 '09 at 23:14
  • 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 Jan 28 '09 at 02:44
  • @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 Jan 28 '09 at 02:58
  • Structs are also unsafe if they are larger than the register size – rpetrich Jan 28 '09 at 11:13
  • Sorry, the difference I was thinking of was the difference between Mac OS X 10.5 and previous. – Paul Tomblin Jan 28 '09 at 13:53
9

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
  • 107,306
  • 24
  • 181
  • 206
2

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
  • 93,976
  • 29
  • 161
  • 209
  • 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 Apr 06 '09 at 10:59