IOS / Objective C:应用程序崩溃核心数据 - 按下保存按钮

时间:2017-02-10 00:25:29

标签: ios objective-c core-data

我正在按照本教程(https://www.appcoda.com/introduction-to-core-data/)关于将核心数据提取到表格视图中,保存和删除新项目。我相信代码是一样的,但应用程序崩溃的原因是:

[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x7b026800'

这是TableViewControll的代码:

#import "TableViewController.h"
#import "DetailViewController.h"
#import "AppDelegate.h"


@interface TableViewController ()
@property (strong) NSMutableArray *lisbonSpots;
@end

@implementation TableViewController
- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
     if ([delegate performSelector:@selector(managedObjectContext)]) {
     context = [delegate managedObjectContext];
     }
    return context;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Spot"];
    self.lisbonSpots = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];

}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return self.lisbonSpots.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSManagedObject *ls = [self.lisbonSpots objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [ls valueForKey:@"name"]]];


    return cell;
}

还有DetailView:

#import "DetailViewController.h"
#import "TableViewController.h"
#import "AppDelegate.h"

@interface DetailViewController ()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *spot;

@end

@implementation DetailViewController

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}


- (IBAction)cancel:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)save:(UIBarButtonItem *)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Spot" inManagedObjectContext:context];
    [newDevice setValue:self.spot.text forKey:@"name"];


    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    _spot.delegate=self;

}
@end

为什么会出现此错误?我做错了还是教程过时了?

编辑:

尽管第一个答案是正确的 - 应用程序运行 - 另一个问题出现了:当我点击保存按钮时,它会崩溃并显示此日志:

'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Spot''

1 个答案:

答案 0 :(得分:2)

if ([delegate performSelector:@selector(managedObjectContext)]) {

应该是:

if ([delegate respondsToSelector:@selector(managedObjectContext)]) {