-1

Possible Duplicate:
Compare 2 string in objective-C

i try to compare equality between 2 NSString ...

the 1st one is stored in the shared user defaults and the 2nd one is entered by the user thru a NSTextField ...

is here is a little bit of my code (Xcode 4.5.2 Mac OS 10.7) ...

1st the AppDelegate.h :

@interface AppDelegate : NSObject <NSApplicationDelegate>{

    NSUserDefaults *administratifPref;

    ...

    IBOutlet NSTextField *champProtection;

    ...
}

...

- (IBAction)poursuivre:(id) sender;

...

@end

and here the AppDelegate.m :

- (IBAction)poursuivre:(id)sender {

if([champProtection stringValue] == [champProtection stringValue]){
    ...
    ...
    }

}

my question is : why the condition "if" is never verified ?!

i get no issues, no crash ...

i did add 2 NSLog :

- (IBAction)poursuivre:(id)sender {


NSLog([champProtection stringValue]);
NSLog([administratifPref valueForKey:@"motdepasse"]);


if([champProtection stringValue] == [champProtection stringValue]){
    ...
    ...
    }

}

and the returned values are the same :(

the only solution i found is doing :

- (IBAction)poursuivre:(id)sender {

BOOL result = [[champProtection stringValue] isEqualToString:[administratifPref valueForKey:@"motdepasse"]];
if(result == YES) {
    ...
    ...
    }

}

so ... can anyone explain me the difference between these 2 ways of coding, that seem to be soooooo different ? (but that really seem the same for a newbee like me, who trust people who say that Cocoa is very simple ^^)

Community
  • 1
  • 1
Naja
  • 311
  • 2
  • 9
  • 1
    Couldn't you just Google this? Asked zillion times. –  Dec 17 '12 at 18:33
  • Agree with @H2Co3, though I gave you a very simple answer. So google it next time. :) – Kjuly Dec 17 '12 at 18:37
  • 1
    Duplicate of : http://stackoverflow.com/questions/6118574/compare-2-string-in-objective-c, http://stackoverflow.com/questions/9322457/comparison-of-two-strings-fails, http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison-in-objective-c – CBredlow Dec 17 '12 at 18:41

4 Answers4

2

The == operator is comparing the objects' pointer address (they are different since you have two objects. Each object has allocated its own memory space and therefore they have different pointer addresses), while isEqualToString is comparing the contents of the memory buffers (i.e. the strings themselves)

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • sorry to say that google does NOT give a so clear answer that mprivat did (thanks man !) – Naja Dec 17 '12 at 20:28
  • and maybe programmers that have years of coding behing them could think that it can be nice, sometimes, to understand that there are newbees like me ^^ too, and that ther were newbees themselves once ago (sorry for my poor english) – Naja Dec 17 '12 at 20:31
2
[champProtection stringValue] == [champProtection stringValue]

Here you are comparing with their addresses.

[[champProtection stringValue] isEqualToString:[administratifPref valueForKey:@"motdepasse"]];

And it is comparing the real string content.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
1

Objective-C is a strict superset of C. It adds objects by placing them behind opaque pointers — the asterisk in all Objective-C object declarations (typedefs aside) means that the actual thing you're holding is a pointer to an object, i.e. its address in memory, rather than the object itself.

When you perform == on two pointers you ask 'do they both reference the same location in memory?'. If they do then both refer to the same object, so amongst other properties both pointers reference something of the same value.

When you use isEqual: (or one of its more specific siblings) you ask 'regardless of their location, do these two objects have the same value?', which is quite a different thing semantically.

So the two things are quite different and in your case your description of the behaviour you want means you want to use the latter.

Tommy
  • 99,986
  • 12
  • 185
  • 204
1

To compare two string you have to use the follow code:

if([name1 isEqualToString:name2]){
    NSLog("These are the same name");
}

You can see more on Apple Documentation and here Difference between == and equals

Community
  • 1
  • 1
Fabio Cardoso
  • 1,181
  • 1
  • 14
  • 37