If I need to compare a NSString property with a constant string define by #define, can I use double equal sign?
I know that isEqualToString: would work, but just wonder if == also works, say for the following two scenarios:
Scenario 1:
#define BLA @"BLA"
NSString *str1 = BLA;
BOOL equal = self.someStr == BLA;
Scenario 2:
#define BLA @"BLA"
NSString *str1 = @"BLA";
BOOL equal = self.someStr == BLA;
are they the same?
==andisEqualToString:are never the same. While==may sometimes behave as if it is checking the actual characters in a string, it is completely by chance. The LLVM compiler heavily optimizes string constants however LLVM optimizations are an implementation detail and are subject to change at any time.isEqualToString:– compares individual characters in a string.==– straight up pointer comparison. This only returns true when bothNSStringobjects are actually the same instance at the same exact memory location.Edit:
#definestatements are evaluated before the compiler runs (they are evaluated in the preprocessor).#definestatements do a straight up search and replace so putting the same thing instead of a#defineis the same exact thing.