The discussions I found about setting NSString constants made me code it the following way:
.h file:
extern NSString * const kSectionHeaders;
.m file:
NSString * const kSectionHeaders = @"header";
As the program runs, it has to test words from a text file against a series of NSString constants.
I read memory comparison should work when setting function like stated above:
if (property == kSectionHeaders) {...}
Doesn't work tough :( The following works, but it was described as a bad solution (slower, what else?):
if ([property isEqualToString:kSectionHeaders]){...}
I feel I've done something wrong. But can't see what! Please help :-) Thanks! J.
==
does pointer comparison, it won't compare the values of two objects. isEqualToString:
(and in general isEqual:
) is the right way to do this - where was it described as a "bad solution"?
Remember variable names are just pointers to objects in memory.
The ==
operand compares the pointers. It will not be true unless it is comparing the exact same object in memory.
isEqualToString:
is your best bet. Don't worry too much about speed. The devices are plenty fast enough to do the comparison in the blink of an eye. The things that really take noticible time are drawing on screen and reading from disk.
Who described that as a bad solution? It is the only proper/correct solution to the problem at hand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With