Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

cocoa touch - Compare two NSStrings

In my app there is a mechanism that requires that at a certain point two NSStrings will be the same to do something; for some reason when I compare the two, even when they are the same, it still doesn't recognize that. The code is something like this:

NSString * aString = [self someMethodThatGetsAString];

NSString * bString;

BOOL areStringsTheSame = NO;

while (areStringsTheSame != YES) {

       bString = [self someMethodThatTakesNSStringsFromAnArrey];
       if (bString == aString) {
             areStringsTheSame = YES;
       { }

I even inserted an NSLog() and made sure that at a certain point they were the same (and as far as I know this is what == stands for...), but still it didn't get into the if to change the BOOL value.

Is there another way to do this comparison? Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the method isEqualToString::

if ([bString isEqualToString:aString])

== compares the references (addresses of) the strings, and not the value of the strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...