-1

I have the following code:

if (flag == 2 && username1 != username2 && password1 != password2)
{
 alert =[[UIAlertView alloc ] initWithTitle: @"INCORRECT...

Values I have:

flag = 2 , username1 = @“aa“, password1 = @“aa“, username2 = @“aa“, password2 = @“aa“

So the above IF statement should evaluate to false right? Yet for reasons unbeknownst to me, the statements after the IF statement are executed! Any idea why?

Thanks for your input.

Larme
  • 24,190
  • 6
  • 51
  • 81

2 Answers2

4

If username1 and the other strings are just char * variables, the pointers can be different even though the content is the same.

In C, you would use strcmp for this but, in ObjC, I think you have to use the -isEqualToString: method, something like:

if ((flag == 2) &&
    (![username1 isEqualToString:username2]) &&
    (![password1 isEqualToString:password2]))
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

To compare the string content instead of the object, use Equal:

if ([string isEqualToString:@"Some String"])
{
    // Do stuff...
}
Kai Mattern
  • 3,090
  • 2
  • 34
  • 37