views:

3093

answers:

1

Following is Objective-C code where I'm trying to do a compare between two NSString values, however it throws a runtime error. Here's the code:

NSDictionary *innerContent=[JSONResponseDict valueForKey:@"JSONRESPONSE"];
NSString *authFlag = [innerContent valueForKey:@"authenticationFlag"];

NSLog(@"authFlag = %@",authFlag);

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:nil
                      message: [NSString stringWithFormat:@"authenticationFlag = %@",authFlag]
                      delegate:self
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
[alert show];
[alert release];

// This block is problematic
if ( [authFlag isEqualToString:@"1"]){
    NSLog(@"Logged in");
    self.view = homeView;
} else {
    NSLog(@"Not logged in");
}

Note that the NSString authFlag has been tested as indeed having a valid string value. authFlag either has a value of "1" or "0" (it's gotten from a response to a JSON call using json-framework).

Here's the runtime error:

[Session started at 2009-03-29 19:21:00 -0700.]
2009-03-29 19:21:11.186 taggle[4144:20b] [email protected]&password=opensesame
2009-03-29 19:21:11.653 taggle[4144:20b] authFlag = 1
2009-03-29 19:21:11.655 taggle[4144:20b] *** -[NSCFBoolean isEqualToString:]:       unrecognized selector sent to instance 0xa089c400
2009-03-29 19:21:11.661 taggle[4144:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFBoolean isEqualToString:]: unrecognized selector sent to instance 0xa089c400'
+5  A: 

As the error log shows, authFlag is not an NSString, but an NSCFBoolean.

You can do this:

NSCFBoolean *authFlag = [innerContent valueForKey:@"authenticationFlag"];

if([authFlag boolValue]) {
    NSLog(@"Logged in");
    self.view = homeView;
} else {
    NSLog(@"Not logged in");
}
Can Berk Güder
That's exactly what I was thinking based on the error, but how can authFlag be boolean when it's typed as an NSString? Is there an implicit conversion/casting going on from the time I declare and instantiate the var to the time I do the compare?
mibrop
[innerContent valueForKey:@"authenticationFlag"] returns a pointer to an NSCFBoolean. You're just storing the pointer value in an NSString*. That doesn't make the pointed object a string.
Can Berk Güder
You can just as easily replace NSString with NSArray or even UITableView. The result doesn't change. The object being pointed to is still an NSCFBoolean.
Can Berk Güder
Declared types for Objective-C objects are purely so the compiler can warn you about mismatches between two declared types. It makes no difference to the actual object. Keep in mind that these are all pointers — the actual object is not stored there, just a pointer to its address.
Chuck
ok, got it now, appreciate everyone's comments.
mibrop
I thought the "type" kept track of both: *WHERE* the object is stored... and... the amount of *SPACE* it needs. No?
Bonnie