Absolutely baffling issue where if I try and compare a string in a mutable array from an external plist to a string, identical strings refuse to admit that they are the same. However, if I create the mutable array directly with the string in it, and then attempt a compare, it says they are the same.
An example is probably easier:
NSString *dragDocPath = [documentsDirectory stringByAppendingPathComponent:@"draggable.plist"];
NSMutableArray *dragArray1 = [[NSMutableArray alloc] initWithContentsOfFile:dragDocPath];
NSString *s1 = [dragArray1 objectAtIndex:0]; //This is <string>skip</string> and checked with logging
NSString *s2 = @"skip";
if (s1==s2) { //do this
}
else { //do the other important thing
}
Without fail, it does the latter and refuses to see that s1==s2
However, I then did
NSMutableArray *dragArray1 = [[NSMutableArray alloc] init];
[dragArray1 addObject:@"skip"];
NSString *s1 = [dragArray1 objectAtIndex:0];
NSString *s2 = @"skip";
and that turned out ok. I’ve noticed this in a previous test project, but assumed i was doing something obviously wrong so left it.
I’m very confused
my plist file is (and they work for every other use i can find for the arrays in them)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>skip</string>
</array>
</plist>
Any help, pointing out obvious flaws etc… would be very appreciated
i should add that I’ve logged it all to the nth degree at each step, with it saying “1st is skip”, “2nd is skip” and “skip is not equal to skip”…
EDIT: Problem solved thanks to answer below. Always always use isEqualToString
eg:
if([s1 isEqualToString:s2]){….
That’s not how you compare strings, you’re actually comparing the pointers (which are different). You should use isEqualToString: