保存核心数据上下文时"unrecognized selector sent to instance ***"异常



我在尝试保存上下文时遇到此异常:

无法识别的选择器发送到实例0x5937b60

我有另一个UITableViewController的类似代码,运行良好。我将两者进行了比较,以确保我做事的方式相同,乍一看,它们看起来几乎完全相同。奇怪的是,在抛出异常之后,我的应用程序确实保存了上下文。当我重新运行它时,我可以看到对我的模型所做的更改。

我用代码构建了所有的接口(没有IB(。

我读到这个问题通常与CoreData试图保存的内容无关,而是与其他一些委托向上下文发送无法理解的消息有关。我不知道这到底发生在哪里。

堆栈看起来像这样:

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

这是应用程序崩溃的方法:

- (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];
}

我不知道应该再附加哪些代码,因为我不确定是什么原因导致了问题。任何帮助都将不胜感激!!!

三次检查选择器的拼写/大小写。看起来你可能正在为一个实际上并不存在的方法添加一个观察者,而且由于XCode无法为你验证选择器,通常会有一个愚蠢的拼写错误导致这类崩溃。

此外,三重检查如果方法需要一个对象,则在选择器的末尾有一个:;同样,如果方法不需要对象,则确保没有对象。:意义重大。

请检查此处显示的代码以外的代码。

相关内容

最新更新