-3

Possible Duplicate:
Why is my comparing if statement not working?

- (NSInteger)setSegment:(NSString *)segmentValue{
NSLog(@"setSegment began");
NSLog(@"Input Value: %@", segmentValue);
NSInteger var = -1;
if (segmentValue == @"Yes"){
    var = 1;
    NSLog(@"%d", var);
}
else if (segmentValue == @"East"){
    var = 1;
    NSLog(@"%d", var);
}
else if (segmentValue == @"No"){
    var = 0;
    NSLog(@"%d", var);
}
else if (segmentValue == @"North"){
    var = 0;
    NSLog(@"%d", var);
}
else if (segmentValue == @"South"){
    var = 2;
    NSLog(@"%d", var);
}
else if (segmentValue == @"West"){
    var = 3;
    NSLog(@"%d", var);
}
NSLog(@"%d", var);
return var;

}

When i execute this method it logs the correct values, and returns -1 every time. The if statements don't execute even when they should return true.

Community
  • 1
  • 1
user1899201
  • 123
  • 1
  • 1
  • 8
  • 1
    Please **do learn C properly** before attempting iOS or OS X development. This is something very fundamental. –  Feb 03 '13 at 10:17
  • It's an honest mistake, many good people, myself included, have made it. It's our place to be helpful and guide.. – talkol Feb 03 '13 at 10:20
  • 1
    This has been asked and answered hundreds of times before on StackOverflow and I'm surprised this hasn't been marked as a dupe yet. – dreamlax Feb 03 '13 at 12:15

3 Answers3

3

Try comparing your NSString with isEqualToString. Reference here: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

talkol
  • 12,564
  • 11
  • 54
  • 64
  • Thanks, I've been trying to figure it out for hours. It's the simple things that get me. – user1899201 Feb 03 '13 at 10:22
  • @user1899201: If you've been at this for hours it may be a good indication that you need to read a good book on Objective-C before continuing. This sort of mistake is so easily avoided by simply reading about Objective-C and looking at some basic examples. – dreamlax Feb 03 '13 at 12:17
  • I have read about it, but i'm used to c and c++, i got them confused. – user1899201 Feb 07 '13 at 13:34
2

This is not C++ with overloadable operators. == always does a numeric comparison on its operands, and in case of NSString *, which is a pointer type, means pointer equality. You must use the isEqualToString: method to compare NSString objects.

1

This is a classic mistake and can occur in many programming languages. The issue is that references to string objects are pointers to the address of the object in memory and your code compares the pointer values and not the content of the string, which is what you intended.

For example

NSString *a = @"Hello";
NSString *b = @"World";

Now imagine that a has the address 0x12345678 and b has the address 0x12348765 then what your code is effectively doing is:

if (0x12345678 == 0x12348765)
{
    ...
}

To compare the content of strings, use the NSString method isEqualToString (reference).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242