1

Possible Duplicate:
Understanding NSString comparison in Objective-C

I want to compare an int and a string, but my code won't work. Here is my code:

NSLog(@"enterPressed was called");
int answer = firstNumber + secondNumber;
NSLog(@"Real Answer: %d", answer);

NSString *numberFromTextField = inputTextField.text;
NSLog(@"Number From Text Field: %@", numberFromTextField);

if (numberFromTextField == [NSString stringWithFormat:@"%d", answer]) {
    NSLog(@"Correct!");
}

And here is the console:

2012-12-09 13:10:55.087 MathQuiz[10728:c07] enterPressed was called

2012-12-09 13:10:55.087 MathQuiz[10728:c07] Real Answer: 60

2012-12-09 13:10:55.088 MathQuiz[10728:c07] Number From Text Field: 60

Why doesn't it say "Correct!"?

Community
  • 1
  • 1
Chiapuz79
  • 33
  • 7
  • 2
    Ouch. This hurts. I mean, seriously. Please learn C properly and read about some general good programming practices before attempting to make *The Best iOS App Ever (TM)*... –  Dec 09 '12 at 21:13
  • I'm teaching myself Objective-C. This is the learning process. Nowhere in my question did I say that I am an expert so don't criticize me for not knowing everything that an expert would. – Chiapuz79 Dec 09 '12 at 21:15
  • @H2CO3 I disagree. Being self-taught myself, I think a great way to learn is just to plunge in and figure it out as you go. Sure, you have to learn some good programming practices along the way, but you will be more motivated, and you will be better able to remember what you have learned, if you "just do it". – DOK Dec 09 '12 at 21:15
  • @DOK "Being self-taught myself" - me too. I have never learnt this at school. Yet I don't have questions like this on SO (but a lot in my Google search history). –  Dec 09 '12 at 21:17
  • Also, learning C has nothing to do with this question, as C doesn't use the `NSString` class. `:)` – apaderno Dec 09 '12 at 21:18
  • @H2CO3 Well if you think that you have it all figured out how would you suggest I learn? – Chiapuz79 Dec 09 '12 at 21:19
  • @Chiapuz79 The trick I found the most useful is when something just didn't work, and I got no more ideas, and I didn't even have the slightest clue for a concise search keyword, I told myself: "Come on, this **must** be something obvious!" - And eventually I had figured it out. –  Dec 09 '12 at 21:22
  • @H2CO3 I don't follow you. Correct me if I'm wrong, but basically what you're saying is stare at a problem until it makes sense? – Chiapuz79 Dec 09 '12 at 21:27
  • @Chiapuz79 Pretty much yes :) –  Dec 09 '12 at 21:28
  • 1
    @kiamlaluno: C doesn't have `NSString`, but it has the `==` operator and it has pointers, and the behaviour for `==` with pointers is the same in C and in Objective-C :) – dreamlax Dec 09 '12 at 21:29
  • @H2CO3 haha alright I'll try that then – Chiapuz79 Dec 09 '12 at 21:30
  • @H2CO3 In this case, the answer is using a method of the `NSString` class. He could know well C, but that would not help him to know which method of `NSString` to use. – apaderno Dec 09 '12 at 21:53

2 Answers2

2

You can't use == to compare two Objective-C objects unless you want to know whether the two objects are identical (that is, both sides of == refer to exactly the same object, not two distinct objects with the same value).

To compare whether strings are equal, you have to use:

if ([str1 isEqualToString:str2])

// or, compare to a constant string:

if ([str1 isEqualToString:@"String 2"])

// or, compare to a string made up on the spot:

if ([str1 isEqualToString:[NSString stringWithFormat:@"%d", answer])

However, in your scenario, it is probably better to compare two integers rather than two strings, so do to this, you need to convert the string from your text field to an integer:

int num = [numberFromTextField intValue];

if (num == answer)
{
    NSLog(@"Correct!");
}

Please note that intValue will return 0 when the string doesn't contain a valid number, so if the answer is 0, and the user enters an invalid number, it will be determined to be correct when technically it isn't. Consider using an NSNumberFormatter or even an NSScanner to determine whether the input is valid before comparing it to answer in any way.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
1

You need to compare it as,

if ([numberFromTextField intValue] == answer)

(numberFromTextField == [NSString stringWithFormat:@"%d", answer]) is comparing the memory address of both strings. Not the values. The following would have given you the right values,

if ([numberFromTextField isEqualToString:[NSString stringWithFormat:@"%d", answer]])
iDev
  • 23,310
  • 7
  • 60
  • 85