If “a == b” is false when comparing two NSString objects

前端 未结 5 1651
梦毁少年i
梦毁少年i 2020-11-27 23:21

I have a class with an accessible method that passes back an NSString when called.

[MyClass getMyString]

The string variable

5条回答
  •  臣服心动
    2020-11-28 00:20

    You're assuming that the C == operator does string equality. It doesn't. It does pointer equality (when called on pointers). If you want to do a real string equality test you need to use the -isEqual: method (or the specialization -isEqualToString: when you know both objects are strings):

    if ([mySecondString isEqualToString:myString]) {
        i = 9;
    }
    

提交回复
热议问题