tags:

views:

70

answers:

5

I am using isEqualToString to compare two strings? as follows:

   if ( [ somethingString isEqualToString: @"MyString" ]   )
   { //do something } else { //do something else }

Now, I have examined somethingString with NSLog and it appears ok in that it shows what I expected. But, the if statement is not working.

I am thinking that even though the Debugger shows the object there before the if is executed, the type or something else is amiss.

The somethingString comes from the UIPickerView which is populated with an array of objects...if this makes any difference.

Any thoughts?

ps. I have tried using the compare: and that definitely does not work.

A: 

you should do if ([somethingString compare: @"MyString"] == NSOrderedSame) { //do stuff }

jamone
What's wrong with isEqualToString: for string comparison...?
Kalle
I have tried compare and I also tried your suggestion compare with NSOrderedSame.Compare actually ranks strings, potentially comparing somethingString and SomethingString as different unless you option ignore capitalization. You would think compare my work in my case because I want an exact comparison but it did not.
MadProfit
A: 

You are not providing enough information. If somethingString is a subclass of NSString (if not, it probably wouldn't implement isEqualToString: in the first place) and its value is equal to "MyString", then [somethingString isEqualToString: @"MyString"] will return YES.

MrMage
I declared somethingString as follows:NSString *somethingString;
MadProfit
A: 

Can you post a simple, self-contained example of the code that does not work as you expect? isEqualToString is the right method to call, the problem will be somewhere else. Are you sure that both objects are valid? Maybe one of them is already deallocated or nil. Aren’t you confusing pointer equivalence (foo == bar) with content equivalence ([foo isEqualToString:bar])?

zoul
Thanks zoul for any help or suggestions. The code and log is contained in this thread.
MadProfit
I seek content equivalence not pointer equivalence and I even checked the addresses of the objects to make sure something wasn't amiss by having multiple Mother s running around my app confusing things...unlike real life with more than one mother/in-law??!
MadProfit
A: 

As requested by zoul, here is the code and the log....

mother = @"Mother";

if ( [ savedFirstNotice isEqualToString: mother ] )

    { NSLog ( @"Mother was triggered") }

    else 

    {      NSLog (@"Mother was not choosen");
           NSLog (@"savedFirstNotice is:  %@", savedFirstNotice);
           NSLog (@"mother is:  %@", mother);
    }

2010-08-02 20:34:16.337 SafetyKnight[4492:307] Mother was not choosen

2010-08-02 20:34:16.338 SafetyKnight[4492:307] savedFirstNotice is: Mother
2010-08-02 20:34:16.339 SafetyKnight[4492:307] mother is: Mother

Also note that savedFirstNotice has been declared as an NSString. It would appear that Mother is equivalent to Mother and the "Mother was triggered" should have been the correct response.

MadProfit
I just performed a CFEqual test between savedFirstNotice and mother, the result is that they are NOT Equal even though the log output shows Mother and Mother. Now,I need to figure out what this all means.... –
MadProfit
Maybe savedFirstNotice contains an additional space at the end, e.g. is equal to @"Mother "?
MrMage
A: 

For those newbies having trouble figuring out what isEqualToString is doing, I did this to solve my problem.

(1) Check to see if the two strings are CFEqual. (2) In both cases, parse the string character by character. In my case, the two strings were not of equal length which did not make sense from the debugger to me. After parsing the savedFirstNotice string, for whatever reason, the app was adding one blank space before the string and two blank spaces after the string. So far, it appears to be a consistent action so it is easy for me to eliminate it from all my variables that I use in the method.

Thanks everybody for your suggestions - ones that work and do not work for it gives me ideas or thoughts to think about.

Here is the code that found my problem:

if (CFEqual (savedFirstNotice , mother ))
    { NSLog (@" Both savedFirstNotice and mother are Equal") ; }
      else
      { NSLog ( @" savedFirstNotice and mother are NOT Equal ") ;
    NSLog ( @" The length of savedFirstNotice is %d", savedFirstNotice.length );
    NSLog ( @" The length of mother is %d", mother.length );
    NSLog ( @" savedFirstNotice character 0 %c ",[ savedFirstNotice characterAtIndex: 0 ]);
    NSLog ( @" savedFirstNotice character 1 %c ",[ savedFirstNotice characterAtIndex: 1 ]);
                       // duplicate to capture the entire string
          }

Also, I recommend that one reads the Apple documentation on CFEqual and related items.

MadProfit