Both the following comparisons evaluate to true:
1)
@"foo" == @"foo";
2)
NSString *myString1 = @"foo";
NSString *myString2 = @"foo";
myString1 == myString2;
However, there are definitely times where two NSStrings cannot be compared using the equality operator, and [myString1 isEqualToString:myString2] is required instead. Can someone shed some light on this?
The reason why
==works is because of pointer comparison. When you define a constantNSStringusing@"", the compiler uniquifies the reference. When the same constants are defined in other places in your code, they will all point to the same actual location in memory.When comparing
NSStringinstances, you should use theisEqualToString:method:Edit:
initWithString:does not create a new reference any more, you will needinitWithFormat,