Reputation: 145
I'm following this tutorial (https://www.appcoda.com/introduction-to-core-data/) about fetching core data into a table view, save and delete new items. I believe the code is the same, but the app crashes due to:
[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x7b026800'
Here's the code for 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;
}
And the 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
Why do I get this error? Am I doing something wrong or is the tutorial out of date?
EDIT:
Despite the first answer being correct - the app runs - another issue came up: when I hit the save button it crashes with this log:
'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Spot''
Upvotes: 0
Views: 172
Reputation: 162722
if ([delegate performSelector:@selector(managedObjectContext)]) {
Should be:
if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
Upvotes: 2