Why does [someObj msgA:@"xyx"] not give my expected result

  • Folks;

    [someObj msgA:@"xyx"];

    - (void) msgA:(NSString *)p1
    {
    if ( [p1 isEqualToString:@"xyz"] )
    {
    NSLog("Match");
    } else {
    NSLog("No Match");
    }
    RESULT:  No Match

    [someObj msgA:[NSString stringWithString@"xyx"]];
    RESULT:  Match

    I realize the 'bad' nature of the calling code, I just want to
    understand what is going on that makes version 1 not see p1 as an
    equivalent string.  The compiler allows version 1 with no comment and
    at run time there is no type error when the message is passed.

    Clues? Links?
    Steve

    Best Regards,
    Steve

    
  • On Nov 29, 2007 12:53 AM, Steve Cronin <steve_cronin...> wrote:
    > I realize the 'bad' nature of the calling code, I just want to
    > understand what is going on that makes version 1 not see p1 as an
    > equivalent string.  The compiler allows version 1 with no comment and
    > at run time there is no type error when the message is passed.
    >
    > Clues? Links?

    We can't help you unless you show us the actual code that is broken.

    The code you pasted was obviously not the code that you tested. (A) It
    won't compile -- missing '}'. (B) You use two constants -- @"xyz" and
    @"xyx" -- which will always give a response of "No Match".

    Bellow is my rewrite of your case, which works as expected. Your
    problem is elsewhere.

    #import <Foundation/Foundation.h>

    @interface Foo : NSObject {}
    - (void) test:(NSString*)aString;
    @end

    @implementation Foo
    - (void) test:(NSString*)aString {
    if ([aString isEqualToString:@"xyz"])
      NSLog( @"MATCH: %@ == xyz", aString );
    else
      NSLog( @"NO MATCH: %@ != xyz", aString );
    }
    @end

    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Foo *foo = [[[Foo alloc] init] autorelease];

    [foo test:@"xyx"];
    [foo test:@"xyz"];
    [foo test:[NSString stringWithString:@"xyx"]];
    [foo test:[NSString stringWithString:@"xyz"]];

        [pool release];
        return 0;
    }
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today