ddd
ddd

Reputation: 359

[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance

I'm trying to display a view after user accepts location sharing. Here is the code:

-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"Denied");
    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        NSLog(@"Accepted!");
        AlertViewController *aViewController = [[AlertViewController alloc] initWithNibName:@"AlertViewController" bundle:nil];
        aViewController.view.frame = CGRectMake(0, 0, 320, 460);
        aViewController.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.window addSubview:[aViewController view]];
    }
}

But I get *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xee6fc90' error at line aViewController.view.frame = ...

I put breakpoints and verified that aViewController is not 0x00000 after the alloc statement. I can't seem to figure out what the problem is. Please suggest solutions.

Upvotes: 2

Views: 3610

Answers (3)

Steven Fisher
Steven Fisher

Reputation: 44886

The error you're seeing means that you or a framework is trying to get the length of a NSNumber. They don't have a length

To narrow this down, you should use an exception breakpoint:

  1. Switch to the breakpoints navigator.
  2. Click the + button on the bottom left.
  3. Pick Add Exception Breakpoint…
  4. Click Done. (the defaults are Exception: All, and Break: On Throw. Exactly what you want.)

Run and this time the debugger will break when the exception is thrown, rather than waiting for it to be unrolled and the application to be terminated. You'll get a much better idea what's actually happening.

Some possibilities:

  1. You're assigning a NSNumber to a NSString property such as a textview's text. It seems that adjusting the frame on the view is what triggers it, but the bug is in other code.
  2. You're using ARC, and you've got an object in a container class or some other form that's "weak" but not zeroing weak. It's been disposed of and replaced with an NSNumber, but other code is attempting to access the string at that location past its lifetime.
  3. You have a custom view subclass that's treating an NSNumber like a string.

But this speculation is rather useless. Get the debugger stopped where the exception is raised; the problem will probably become obvious.

Upvotes: 3

Chris Trahey
Chris Trahey

Reputation: 18290

I suspect an issue in the xib file or in the AlertViewController's view initialization routine (i.e. viewDidLoad or loadView). The only way I can reason that the line setting the frame is crashing is that it's the first time you are accessing the view property, and views are lazily loaded.

I suspect that if you replaced

aViewController.view.frame = CGRectMake(0, 0, 320, 460);

with

UIView *aVCView = aViewController.view;
aVCView.frame = CGRectMake(0, 0, 320, 460);

then you would see the crash on the first line, not the second.

To debug further, I would use breaks in AlertViewController's viewDidLoad or similar. And double-check everything in the xib.

Upvotes: 3

pasawaya
pasawaya

Reputation: 11595

In your conditional statements, what type is status? You could be using the wrong operator or method. For example, if status is an int, bool, or a float, you would use the ==. If it was a string, you would use isEqualToString, and if it was a generic object, you could use simply isEqual:. There are also specialized versions of the isEqual: function such as isEqualToArray.

Hope this helps!

Upvotes: 0

Related Questions