嗨基于一个教程,我已经在Add New view controller中连接了Done按钮的方法。
文本字段保存回自己的控制器。该程序构建和运行。但是当您单击“完成”按钮以保存新记录时,它会引发错误。该方法似乎输入了文本 - 基于调试器中name NSString * @"jkl" 0x00007f9828d8d6b0
的存在,但NS实体为nil并且崩溃。entity NSEntityDescription * nil 0x0000000000000000
我已经三次检查了实体的名称和它是正确的。
谁能告诉我可能出现的问题?谢谢!
- (IBAction)save:(id)sender {
// Helpers
NSString *name = self.textField.text;
[[[UIAlertView alloc] initWithTitle:@"We got data" message:name delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
if (name && name.length) {
// Create Entity FOLLOWING LINE IS HIGHLIGHTED AND THROWS ERROR
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TSPItem" inManagedObjectContext:self.managedObjectContext];
//END HIGHLIGHTED ERRROR
// Initialize Record
NSManagedObject *record = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
// Populate Record
[record setValue:name forKey:@"name"];
[record setValue:[NSDate date] forKey:@"createdAt"];
// Save Record
NSError *error = nil;
if ([self.managedObjectContext save:&error]) {
// Dismiss View Controller
[self dismissViewControllerAnimated:YES completion:nil];
} else {
if (error) {
NSLog(@"Unable to save record.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
// Show Alert View
[[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your field could not be saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
} else {
// Show Alert View
[[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Your field needs a name." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
编辑:
以下是其中一个主题的其他内容。我在调试方面不够熟练,不知道它意味着什么:
0x10ce9de77: leaq 0x26fa82(%rip), %r8 ; @"+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name '%@'"
0x10ce9de7e: jmp 0x10ce9dece ; +[NSEntityDescription entityForName:inManagedObjectContext:] + 206
0x10ce9de80: movq 0x2c5311(%rip), %r14 ; (void *)0x000000010dce1458: NSException
0x10ce9de87: movq 0x2691ca(%rip), %rax ; (void *)0x000000010dca9a20: NSInvalidArgumentException
0x10ce9de8e: movq (%rax), %r15
0x10ce9de91: movq 0x2c52b8(%rip), %rcx ; (void *)0x000000010d585e90: NSString
0x10ce9de98: movq 0x2be8e9(%rip), %rdx ; "stringWithFormat:"
0x10ce9de9f: leaq 0x26fa7a(%rip), %r8 ; @"+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name '%@'"
0x10ce9dea6: jmp 0x10ce9dece ; +[NSEntityDescription entityForName:inManagedObjectContext:] + 206
0x10ce9dea8: movq 0x2c52e9(%rip), %r14 ; (void *)0x000000010dce1458: NSException
0x10ce9deaf: movq 0x26919a(%rip), %rax ; (void *)0x000000010dca9a28: NSInternalInconsistencyException
0x10ce9deb6: movq (%rax), %r15
0x10ce9deb9: movq 0x2c5290(%rip), %rcx ; (void *)0x000000010d585e90: NSString
0x10ce9dec0: movq 0x2be8c1(%rip), %rdx ; "stringWithFormat:"
0x10ce9dec7: leaq 0x26fa72(%rip), %r8 ; @"+entityForName: could not locate an NSManagedObjectModel for entity name '%@'"
0x10ce9dece: movq 0x2692eb(%rip), %r12 ; (void *)0x000000010d6e9000: objc_msgSend
0x10ce9ded5: xorl %eax, %eax
0x10ce9ded7: movq %rcx, %rdi
0x10ce9deda: movq %rdx, %rsi
0x10ce9dedd: movq %r8, %rdx
0x10ce9dee0: movq %rbx, %rcx
0x10ce9dee3: callq *%r12
0x10ce9dee6: movq 0x2be55b(%rip), %rcx ; "exceptionWithName:reason:userInfo:"