问题
I make a comparison of two strings which are obviously identical. Unfortunately my if clause doesn´t work as expected:
NSLog(@"%@ == %@ ?",strippedString1,strippedString2);
if (strippedString1 == strippedString2) {
[_newBuild setTextColor: [NSColor greenColor] ];
[_OldBuild setTextColor: [NSColor greenColor] ];
}
This is my NSLog output: Build: 2A12-046 == Build: 2A12
Even a byte by byte comparison says both strings are identical:

Any clues ?
回答1:
With ==
you are comparing pointer address, to compare the contents of the strings you could use:
[strippedString1 isEqualToString: strippedString2];
回答2:
I think for String, you are supposed to use (This is Java) .equals()
or equivalent.
回答3:
You can't compare two NSStrings with the == operator, instead use
[strippedString1 isEqualToString:strippedString2];
回答4:
Important part: you meant to use isEqualToString
Using == will check for equality of of the pointer, i.e. it will tell you if the two objects you are comparing are actually the same instance.
Instead I think you meant to check that the contents are the same, but accourding to the appledocs you will want to use isEqualToString when you know the two objects are strings as it is much faster than isEqual.
来源:https://stackoverflow.com/questions/9322457/comparison-of-two-strings-fails