我最近在我的应用程序中发布了一个关于错误的问题,但我现在设法用这部分代码修复了这个错误,但我现在得到了同样的错误(主题1:信号SIGABRT )但是对于代码的不同部分。我已经包含了头文件和实现文件,以及main.m文件。我通过在//
之后写下错误来指示错误正在使用哪一行。调试器发出应用程序崩溃原因的消息是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Shift'
我认为这是指我在错误所在的代码行中的@“Shift”,但我不知道为什么它不合法。请你能帮我找出原因。
NameSaveAsViewController.h
#import <UIKit/UIKit.h>
#import "Foundation/Foundation.h"
@interface NameSaveAsViewController : UIViewController {
}
@property (nonatomic, weak) IBOutlet UITextField *saveAsText;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator
*persistantStoreCoordinator;
- (IBAction)saveAs:(id)sender;
- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;
- (IBAction)textFieldDidEndEditing:(UITextField *)textField;
- (IBAction)dismissKeyboard:(id)sender;
@end
NameSaveAsViewController.m
#import "NameSaveAsViewController.h"
#import "AppDelegate.h"
#import "Shift.h"
@interface NameSaveAsViewController ()
@end
@implementation NameSaveAsViewController
@synthesize saveAsText;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistantStoreCoordinator = _persistantStoreCoordinator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
-(void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];
saveAsText.placeholder = @"Shift Name";
}
- (void)dismissKeyboard:(id)sender
{
if ([saveAsText isEditing]) {
[saveAsText resignFirstResponder];
}
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
- (void)saveAs:(id)sender
{
Shift *shift = [NSEntityDescription insertNewObjectForEntityForName:@"Shift" inManagedObjectContext:_managedObjectContext]; //this is the line that xcode tells me has the error
[shift setMake:saveAsText.text];
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
}
saveAsText.text = NULL;
}
@end