comparison of two strings fails

て烟熏妆下的殇ゞ 提交于 2019-12-29 09:08:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!