Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ausername

macrumors newbie
Original poster
Feb 28, 2009
25
0
I've run into a snag... Could someone please tell me why this doesn't work? and show me how I can make it work? thanks :)


#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *str1 = @"abc";
NSString *str2;
str2 = [str1 substringWithRange:NSMakeRange(0,1)];
if (str2 == @"a")
printf("YES");
else
printf("NO");

[pool drain];
return 0;
}



It gives me "NO." I want to get "YES." They're both a's so I don't understand why it says they are not the same. Thanks in advance.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You're comparing the pointer values of each object, instead of the contents of each object. Use this instead:
Code:
if ([str2 isEqualToString:@"a"])

Edit: Lee beat me ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.