Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bguy

macrumors newbie
Original poster
May 30, 2009
28
0
I am writing a Cocoa Program. I have reached a problem when I am trying to compare a user generated string to a constant. Right now I am trying to compare them by using
Code:
if([abc stringValue] == @"xyz")
{
     ...
}
Where abc is an editable text field.
This doesn't work when I type xyz into abc and activate it. I know it is connected correctly as other functions are working. I believe it has to do with the null terminator or something, but I don't know how to fix the problem.
Can someone help me with this?
 
This doesn't work when I type xyz into abc and activate it. I know it is connected correctly as other functions are working. I believe it has to do with the null terminator or something, but I don't know how to fix the problem.

The problem is that you are comparing the string pointers in your example to see if they are pointing at the same object. The hard-coded string will point at a pre-compiled string constant (implementation detail) while the other will point at a different string object.

As kainjow said, the correct approach is to use NSString's isEqualToString method to do the comparison.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.