comparison of two strings fails

后端 未结 4 515
借酒劲吻你
借酒劲吻你 2020-12-21 14:16

I make a comparison of two strings which are obviously identical. Unfortunately my if clause doesn´t work as expected:

NSLog(@\"%@ == %@ ?\",strippedString1,         


        
相关标签:
4条回答
  • 2020-12-21 14:34

    You can't compare two NSStrings with the == operator, instead use

    [strippedString1 isEqualToString:strippedString2];
    
    0 讨论(0)
  • 2020-12-21 14:35

    I think for String, you are supposed to use (This is Java) .equals() or equivalent.

    0 讨论(0)
  • 2020-12-21 14:46

    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.

    0 讨论(0)
  • 2020-12-21 14:49

    With == you are comparing pointer address, to compare the contents of the strings you could use:

     [strippedString1 isEqualToString: strippedString2];
    
    0 讨论(0)
提交回复
热议问题