Pregunta

I have written a some code for a drill down table app I have been making, but the app crashes only during runtime. Xcode doesn't give me any errors while building the app. The debugger outputs:

2012-10-18 10:58:26.513 second[474:c07] -[NavController setItems:]: unrecognized selector >sent to instance 0xc217a00 2012-10-18 10:58:26.515 second[474:c07] * Terminating app due to uncaught exception >'NSInvalidArgumentException', reason: '-[NavController setItems:]: unrecognized selector sent >to instance 0xc217a00' * First throw call stack: (0x14b8022 0xeb8cd6 0x14b9cbd 0x141eed0 0x141ecb2 0x3fbe 0xe2a1e 0x41401 0x41670 0x41836 >0xbfc9dd8 0x4872a 0x19596 0x1a274 0x29183 0x29c38 0x1d634 0x13a2ef5 0x148c195 0x13f0ff2 >0x13ef8da 0x13eed84 0x13eec9b 0x19c65 0x1b626 0x1d40 0x1cd9) terminate called throwing an exception

I think I understand that the error lies in NavController.m where:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    MasterViewController* root = (MasterViewController*)self.topViewController;
    NSDictionary* thelist = [NSDictionary dictionaryWithContentsOfFile:path];
    root.items = [thelist objectForKey:@"Items"];
    root.navigationItem.title = [thelist objectForKey:@"name"];
}

btw, I made the array items like this: (nonatomic, retain) NSArray* items;

¿Fue útil?

Solución

The app crashes, because of the following line:

root.items = [thelist objectForKey:@"Items"];

This line is just a shortcut for writing:

[root setItems:[thelist objectForKey:@"Items"]];

And the runtime complains that there is no method setItems: found. The reason why there is compile error is because you tell the compiler that root is of class MasterViewController, however, that is not true. At runtime the obj-c runtime finds out that root is in fact of class NavController and it seems like NavController has no setItems: method.

In other words, this line is wrong:

MasterViewController* root = (MasterViewController*)self.topViewController;

You lie about the class; self.topViewController returns an object that is of class NavController, yet you force the compiler to treat it as MasterViewController which will fail as soon as you call a method that is not found for NavController.

Otros consejos

The problem is probably here:

MasterViewController* root = (MasterViewController*)self.topViewController;

self.topViewController is proably not an instance of MasterViewController, but a NavController instance and there you go.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top