6

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

[MyClass getMyString]

The string variable in that class is actually assigned in the didSelectRowAtIndexPath: part of a table like this:

myString = cell.textLabel.text;

When I retrieve the string by calling that method, I assign it to another string in the class that called it and compare it to a string I have defined

NSString *mySecondString;
mySecondString = @"my value";

if(mySecondString == myString){
    i = 9;
}

I have stepped through the code and every time it evaluates the if statement, it skips right past the i=9 and goes to the next else if statement. Why would this be? Why don't they evaluate to be the same value? If you hover your cursor over each of the values during debugging they will show they have the same value, but the code for some reason with not do as I expect it to do and assign 9 to i.

Any thoughts?

jscs
  • 63,694
  • 13
  • 151
  • 195
MeisterPlans
  • 129
  • 1
  • 10
  • I'm surprised that a search on this site did not bring up any results. This question has been asked and answered numerous times in the past. – dreamlax Dec 22 '11 at 02:09
  • Perhaps I did not ask it the right way when I searched before I asked... - saw lots of questions that dealt with CFString and assigning it to NSString, but didn't see what Kevin Ballard quickly answered below. Thanks! – MeisterPlans Dec 22 '11 at 02:49
  • possible duplicate of [Understanding NSString comparison](http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison) – jscs Apr 01 '15 at 19:00

5 Answers5

17

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;
}
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
3

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

if (if([mySecondString isEqualToString:myString]) {
    ....
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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

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

IPaPa
  • 368
  • 2
  • 12
1

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.

viral
  • 4,168
  • 5
  • 43
  • 68
1

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 ");

}

parag
  • 657
  • 1
  • 6
  • 17