I learned about core data from : http://www.appcoda.com/introduction-to-core-data/, but when I developed a sample project myself, many errors come up in two files. Any help would be appreciated as I am a newbie in iPhone development
//
// PupilViewController.m
// Pupils
//
// Created by Lukasz Mozdzen on 21.04.2013.
// Copyright (c) 2013 Lukasz Mozdzen. All rights reserved.
//
#import "PupilViewController.h"
@interface PupilViewController ()
@property (strong) NSMutableArray *pupils;
@end
@implementation PupilViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Pupil"];
self.pupils = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.pupils.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *pupil = [self.pupils objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [pupil valueForKey:@"name"], [pupil valueForKey:@"surname"]]];
[cell.detailTextLabel setText:[pupil valueForKey:@"telephone"]];
return cell;
}
@end
Error Log :
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:5: Use of undeclared identifier 'NSFetchRequest'
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:21: Use of undeclared identifier 'fetchRequest'
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:36:38: Use of undeclared identifier 'NSFetchRequest'
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:37:62: Use of undeclared identifier 'fetchRequest'
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:62:5: Unknown type name 'NSManagedObject'; did you mean 'NSManagedObjectModel'?
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:63:67: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration
/Users/Lukasz/Desktop/Pupils/Pupils/PupilViewController.m:64:36: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration
Also in other file:
//
// PupilDetailViewController.m
// Pupils
//
// Created by Lukasz Mozdzen on 21.04.2013.
// Copyright (c) 2013 Lukasz Mozdzen. All rights reserved.
//
#import "PupilDetailViewController.h"
@interface PupilDetailViewController ()
@end
@implementation PupilDetailViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *newPupil = [NSEntityDescription insertNewObjectForEntityForName:@"Pupil" inManagedObjectContext:context];
[newPupil setValue:self.nameTextField.text forKey:@"name"];
[newPupil setValue:self.surnameTextField.text forKey:@"surname"];
[newPupil setValue:self.telephoneTextField.text forKey:@"telephone"];
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];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Error Log :
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:5: Unknown type name 'NSManagedObject'; did you mean 'NSManagedObjectModel'?
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:34: Use of undeclared identifier 'NSEntityDescription'; did you mean 'kSecAttrDescription'?
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:38:34: Bad receiver type 'CFTypeRef' (aka 'const void *')
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:39:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:40:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:41:6: Receiver type 'NSManagedObjectModel' for instance message is a forward declaration
/Users/Lukasz/Desktop/Pupils/PupilDetailViewController.m:45:11: Receiver type 'NSManagedObjectContext' for instance message is a forward declaration
Anyone can help?
You need to add the Framework for coredata in your bundle before using the same.
As you said, you are a newbie in iPhone development, I would suggest you to refer to apple docs on coredata before implementing it.
In addition to adding Core Data framework in project settings, you have to #import <CoreData/CoreData.h>
in your source code. You can do it just once for entire project by placing such #import
into .pch file for your project, typically found in "Supporting Files" group of your project tree.
The problem is, there passed some time since then and since iOS 10, the ManagedObjectContext
has been moved to the PersistentContainer
inside the attribute viewContext
. This is also why you need to change the snippet from AppCoda a little bit so that it will call the context#
-(NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// call "persistentContainer" not "managedObjectContext"
if( [delegate performSelector:@selector(persistentContainer)] ){
// call viewContext from persistentContainer not "managedObjectContext"
context = [[delegate persistentContainer] viewContext];
}
return context;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With