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

Deadly 提交于 2019-11-26 21:03:27

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;
}

You are comparing pointers to strings, rather than the strings themselves. You need to change your code to

if (if([mySecondString isEqualToString:myString]) {
    ....
}

you can not use '==' to compare two NSString

you should to use [NSString isEqualToString:(NSString*)] to compare two string

You can not compare the two string using "==" this is for int and other values. you can use below code for the comparing two string

if ([Firststring isEqualToString:Secondstring]) {

  NSLog(@"Hello this both string is same ");

}

It's a basic concept of pointer, you are missing. (YES, myString and mySecondString are pointers to the string).

Now, if(mySecondString == myString) will go TRUE only if, both the pointers are pointing to the same location. (Which they won't in most cases)

You should be doing if ([mySecondString isEqualToString:myString]), which will be comparing your both string's content for equality.

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