这是我在Xcode 10.1中输入的代码
- (void)createData {
id delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSManagedObject *personObj = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
[personObj setValue:@"Bob" forKey:@"fullname"];
[personObj setValue:@(15) forKey:@"age"];
if ([context save:nil]) {
NSLog(@"Update Person SUCCESS");
} else {
NSLog(@"Update Person FAIL");
}
}
编译项目时,我选中了该复选框,但是出现以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x60800003c200'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107ab8d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001070f921e objc_exception_throw + 48
2 CoreFoundation 0x0000000107b28f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000107a3e005 ___forwarding___ + 1013
4 CoreFoundation 0x0000000107a3db88 _CF_forwarding_prep_0 + 120
5 MyStore 0x0000000106b1ff80 -[DeviceViewController managedObjectContext] + 128
6 MyStore 0x0000000106b20058 -[DeviceViewController viewDidAppear:] + 88
7 UIKit 0x0000000108084a6c -[UIViewController _setViewAppearState:isAnimating:] + 945
8 UIKit 0x00000001080b98d7 -[UINavigationController viewDidAppear:] + 207
9 UIKit 0x0000000108084a6c -[UIViewController _setViewAppearState:isAnimating:] + 945
10 UIKit 0x00000001080877da __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke + 42
11 UIKit 0x0000000108085ac4 -[UIViewController _executeAfterAppearanceBlock] + 86
12 UIKit 0x0000000107ee977c _runAfterCACommitDeferredBlocks + 653
13 UIKit 0x0000000107ed6273 _cleanUpAfterCAFlushAndRunDeferredBlocks + 566
14 UIKit 0x0000000107ef9757 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke_2 + 194
15 CoreFoundation 0x0000000107a5d6ac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
16 CoreFoundation 0x0000000107a426f4 __CFRunLoopDoBlocks + 356
17 CoreFoundation 0x0000000107a41e65 __CFRunLoopRun + 901
18 CoreFoundation 0x0000000107a41884 CFRunLoopRunSpecific + 420
19 GraphicsServices 0x000000010b952a6f GSEventRunModal + 161
20 UIKit 0x0000000107edcc68 UIApplicationMain + 159
21 MyStore 0x0000000106b2132f main + 111
22 libdyld.dylib 0x000000010a9ae68d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我不熟悉在目标c中使用coreData。我已经搜索了详细的堆栈和互联网,但找不到解决方案。