我有一个使用核心数据框架的应用程序,我对它很新。当我输入关于“pilot”的信息并保存它时,该实体的NSLog返回null,我无法弄清楚为什么他们不保存任何帮助将不胜感激。
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <Foundation/Foundation.h>
#import "Pilot.h"
@interface PilotViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate, ABPeoplePickerNavigationControllerDelegate>
@property (weak, nonatomic ) IBOutlet UITextField *nameField;
@property (weak, nonatomic ) IBOutlet UITextField *phoneField;
@property (weak, nonatomic ) IBOutlet UITextField *insuranceField;
@property (weak, nonatomic ) IBOutlet UITextField *emailField;
@property (strong ) NSManagedObject *pilot;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
#import "PilotViewController.h"
@interface PilotViewController ()
@end
@implementation PilotViewController
@synthesize pilot;
#pragma mark Core Data Managed Object
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (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.
}
#pragma mark Save and Cancel Buttons
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
if (self.pilot) {
//Update existing pilot information
[self.pilot setValue:self.nameField.text forKey:@"pilotName"];
[self.pilot setValue:self.phoneField.text forKey:@"pilotPhone"];
[self.pilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
[self.pilot setValue:self.emailField.text forKey:@"pilotEmail"];
} else {
// Create a new managed object
NSManagedObject *newPilot = [NSEntityDescription insertNewObjectForEntityForName:@"Pilot" inManagedObjectContext:context];
[newPilot setValue:self.nameField.text forKey:@"pilotName"];
[newPilot setValue:self.phoneField.text forKey:@"pilotPhone"];
[newPilot setValue:self.insuranceField.text forKey:@"pilotInsurance"];
[newPilot setValue:self.emailField.text forKey:@"pilotEmail"];
{
NSString *pilotName = [pilot valueForKey:@"pilotName"];
NSLog( @"%@",pilotName);
//NSLog(@"Value of pilotName = %@",pilot);
}
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];
}
}
}
@end
答案 0 :(得分:0)
似乎您的托管对象上下文为零。你为什么一开始就把它设置为零?检索它的正确方法是确保将应用代理强制转换为您的应用代理:
AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
return delegate.manageObjectContext;
Apple推荐的模式不使用app delegate。您应该为上下文提供@property
上下文,并从呈现视图控制器传递上下文(例如,在prepareForSegue
中)。