开发者

"unrecognized selector sent to instance ***" exception when saving coredata context

I get this exception when trying to save my context:

unrecognized selector sent to instance 0x5937b60

I have a similar code for another UITableViewController, that's working fine. I compared both to make sure that I'm doing things the same way and at first sight, they look almost identical. Strange is that after throwing the exception my application does save the context. When I re-run it, I can see the changes done to my model.

I built all my interface with code (no IB).

I read that this problem normally doesn't have anything with CoreData trying to save but rather with some other delegate sending messages to the context that this can't understand. I am clueless where that's actually happening.

The stack looks like this:

0   CoreFoundation                      0x00fbd5a9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x01111313 objc_exception_throw + 44
2   CoreFoundation                      0x00fbf0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x00f2e966 ___forwarding___ + 966
4   CoreFoundation                      0x00f2e522 _CF_forwarding_prep_0 + 50
5   Foundation                          0x007aa669 _nsnote_callback + 145
6   CoreFoundation                      0x00f959f9 __CFXNotificationPost_old + 745
7   CoreFoundation                      0x00f1493a _CFXNotificationPostNotification + 186
8   Foundation                          0x007a020e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
9   CoreData                            0x00d295b9 -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] + 1513
10  CoreData                            0x00d2388a -[NSManagedObjectContext save:] + 522
11  TimeManager                         0x0000fe9d -[WorkTimeRootViewController viewController:didFinishWithSave:] + 253
12  TimeManager                         0x00012b73 -[AddWorkTimeViewController save:] + 83
13  UIKit                               0x000384fd -[UIApplication sendAction:to:from:forEvent:] + 119
14  UIKit                               0x0024acc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
15  UIKit                               0x000384fd -[UIApplication sendAction:to:from:forEvent:] + 119
16  UIKit                               0x000c8799 -[UIControl sendAction:to:forEvent:] + 67
17  UIKit                               0x000cac2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
18  UIKit                               0x000c97d8 -[UIControl touchesEnded:withEvent:] + 458
19  UIKit                               0x0005cded -[UIWindow _sendTouchesForEvent:] + 567
20  UIKit                               0x0003dc37 -[UIApplication sendEvent:] + 447
21  UIKit                               0x00042f2e _UIApplicationHandleEvent + 7576
22  GraphicsServices                    0x011f6992 PurpleEventCallback + 1550
23  CoreFoundation                      0x00f9e944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
24  CoreFoundation                      0x00efecf7 __CFRunLoopDoSource1 + 215
25  CoreFoundation                      0x00efbf83 __CFRunLoopRun + 979
26  CoreFoundation                      0x00efb840 CFRunLoopRunSpecific + 208
27  CoreFoundation                      0x00efb761 CFRunLoopRunInMode + 97
28  GraphicsServices                    0x011f51c4 GSEventRunModal + 217
29  GraphicsServices           开发者_运维问答         0x011f5289 GSEventRun + 115
30  UIKit                               0x00046c93 UIApplicationMain + 1160
31  TimeManager                         0x00001c79 main + 121
32  TimeManager                         0x00001bf5 start + 53

This is the method where the application crashes:

- (void)viewController:(id)controller didFinishWithSave:(BOOL)save
{
    if (save) 
    {
        NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
        [dnc addObserver:self selector:@selector(addControllerDidSave:) name:NSManagedObjectContextDidSaveNotification object:self.addingManagedObjectContext];

        NSError *error;
        if (![self.addingManagedObjectContext save:&error]) // it crashes here!
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }

        [dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:self.addingManagedObjectContext];
    }

    self.addingManagedObjectContext = nil;

    [self dismissModalViewControllerAnimated:YES];
}

I don't know which more code I should attach, since I'm not sure what's causing the problem. Any help will be REALLY appreciated!!!


Triple check the spelling / case of your selectors. Looks to be as if you are maybe adding an observer for a method that doesn't actually exist, and because XCode can't validate selectors for you, often there is a silly typo that causes these sorts of crashes.

Also - triple check that if the method expects an object, there is a : at the end of the selector, and equally if the method doesn't expect an object, ensure there isn't one. The : is significant.

Check ones other than the code you have shown here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜