Can you help me understand isEqualToString: ?

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

  1. macrumors member

    Joined:
    Feb 5, 2009
    #1
    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. macrumors member

    Joined:
    Apr 9, 2008
    #2
    Code:
    - (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.

    Code:
    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. thread starter macrumors member

    Joined:
    Feb 5, 2009
    #3
    Great explanation, Rastersize. Thanks very much for the help.
     
  4. macrumors newbie

    Joined:
    Jan 29, 2011
    #4
    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(@"TRUE");
    }
    else
    {
    NSLog(@"FALSE");
    }

    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. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #5
    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. macrumors newbie

    Joined:
    Jan 29, 2011
    #6
    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. macrumors 603

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

    Next, look at the reference doc for the %@ print specifier:
    http://developer.apple.com/library/...fiers.html#//apple_ref/doc/uid/TP40004265-SW1

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

    Joined:
    Jan 29, 2011
    #8
    reply

    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