Can you help me understand isEqualToString: ?

Discussion in 'Mac Programming' started by Glama, Feb 11, 2009.

  1. Glama macrumors member

    Feb 5, 2009
    Hello, The book I'm studying on Objective-C says that isEqualToString: is declared like this:

    - (BOOL) isEqualToString: (NSSTRING *) aString;

    I know that the BOOL means it returns a BOOL value, but I dont know how to read the rest of the line. The * means that NSSTRING is a pointer, correct? what is aString?

    The book has this example on how to use it:

    if ([thing1 isEqualToString: thing2]) {
    NSLog (@"They are the same");

    Again I am confused as to how to read the bolded line and relate it to the declaration. It seems weird that a parameter (thing1) is before the function (isEqualToString:). Can anyone help me understand the bolded line?
  2. rastersize macrumors member

    Apr 9, 2008
    - (BOOL) isEqualToString: (NSString *) aString;
    "isEqualToString:" is a method which takes a pointer ("aString") to a NSString object and compares it to the NSString object it was called on.

    if ([thing1 isEqualToString: thing2]) {
    Here "thing1" is a pointer to a NSString object and we use it's member method "isEqualToString:" to compare it with the NSString object "thing2".

    So "thing1" isn't an argument but an object which has a member method (or function if that's easier) called "isEqualToString:"
  3. Glama thread starter macrumors member

    Feb 5, 2009
    Great explanation, Rastersize. Thanks very much for the help.
  4. ALEXROD macrumors newbie

    Jan 29, 2011
    How About This

    I'm having the same problem but it does not work, can you tell me what's wrong. (note:test 3 is the output from the keyboard dictionary it's a frame reference of the keyboard view.)

    NSString *test3=[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    NSString *test2 = @"NSRect: {{0, 217}, {320, 263}}";

    NSLog(@"value is-%@",test3);
    NSLog(@"string is-%@",test2);

    NSlog output is…. you see they are both equal testing should evaluate to true

    2011-01-29 09:48:30.314 APFA5[15478:207] value is-NSRect: {{0, 217}, {320, 263}}
    2011-01-29 09:48:30.315 APFA5[15478:207] string is-NSRect: {{0, 217}, {320, 263}}

    but if I evaluate with...

    if ([test2 isEqualToString:test3]){


    NSLog output becomes…..

    2011-01-29 09:58:25.300 APFA5[15701:207] value is-NSRect: {{0, 217}, {320, 263}}
    2011-01-29 09:58:25.301 APFA5[15701:207] string is-NSRect: {{0, 217}, {320, 263}}
    2011-01-29 09:58:25.301 APFA5[15701:207] FALSE

    it evaluates as FALSE WHY, WHY, WHY, WHY!!!!!!!!! HOW DO I COMPARE THESE NSString values?
  5. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    NSRect is not a string, so you cant make a literal comparison. You may try isEqualTo instead, or comparing the actual values of the NSRect with each other.
  6. ALEXROD macrumors newbie

    Jan 29, 2011
    Thanks Sayer

    Thanks Sayer I appreciate your response. You gave me a good idea to compare frames instead of strings.

    I tried isEqualToString but it always evaluates to FALSE even both values are equal. (Probably comparing memory pointers and not values)

    But What I cant understand is that both values are output by NSLog as strings to the console, so how does NSLog do that. That would equalize both values so they can be compared...

    Anyway thanks, again...
  7. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    First, look at your formatting string:
    @"value is-%@"
    What are the print specifiers(the segments that begin with %)?

    Next, look at the reference doc for the %@ print specifier:

    What does it say about %@?
  8. ALEXROD macrumors newbie

    Jan 29, 2011

    Ah, I see, It says I was trying to stuff an Elephant into a Pickle jar. I printed out the page, thanks for the reference...

Share This Page