I have a class with an accessible method that passes back an NSString
when called.
[MyClass getMyString]
The string variable
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 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 ");
}
you can not use '==' to compare two NSString
you should to use [NSString isEqualToString:(NSString*)] to compare two string
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.
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;
}