我需要从我的AppDelegate获取de ObjectManagedContext,但是当我尝试它时不起作用...我不知道为什么...我遵循了一系列教程但它不起作用....这是我的代码:
AppDelegate.h
#import <UIKit/UIKit.h>
@class StopWalletViewController;
@interface StopWalletAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (strong, nonatomic) StopWalletViewController *viewController;
@property (strong,nonatomic) UINavigationController *navigationController;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m
#import "StopWalletAppDelegate.h"
#import "StopWalletViewController.h"
@implementation StopWalletAppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize navigationController = _navigationController;
//Code Auto Generated
// Returns the managed object context for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"StopWallet" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"StopWallet.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
@end
Controller.m或者
#import "ExpenseViewController.h"
#import "Expense.h"
#import "ExpenseLocation.h"
#import "StopWalletAppDelegate.h"
@implementation ExpenseViewController
@synthesize editTextDate;
@synthesize editTextLocation;
@synthesize editTextValue;
@synthesize imageViewReceipt;
@synthesize managedObjectContext;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title=[NSString stringWithFormat:@"Expense"];
UIDatePicker *datePicker;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker removeTarget:self action:nil forControlEvents:UIControlEventValueChanged];
[datePicker addTarget:self action:@selector(updateTextFieldDate:) forControlEvents:UIControlEventValueChanged];
[editTextDate setInputView:datePicker];
StopWalletAppDelegate *appDelegate = (StopWalletAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = appDelegate.managedObjectContext;
managedObjectContext=context;
}
@end
此代码正在编译但不适用于运行时....错误: NSInvalidArgumentException,原因:'无法使用nil模型创建NSPersistentStoreCoordinator'
任何人都可以帮助我吗?
...谢谢
答案 0 :(得分:0)
问题应该在- (NSManagedObjectModel *)managedObjectModel
。检查modelURL
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
// Give proper name of your model here
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FailedBankCD" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
答案 1 :(得分:0)
尝试将managedObject上下文直接分配给appdelegate。见下面的粗线。你还可以分享你的ExpenseViewController.h文件吗?只是好奇看你如何定义你的managedObjectContext属性。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title=[NSString stringWithFormat:@"Expense"];
UIDatePicker *datePicker;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker setDatePickerMode:UIDatePickerModeDate];
[datePicker removeTarget:self action:nil forControlEvents:UIControlEventValueChanged];
[datePicker addTarget:self action:@selector(updateTextFieldDate:) forControlEvents:UIControlEventValueChanged];
[editTextDate setInputView:datePicker];
StopWalletAppDelegate *appDelegate = (StopWalletAppDelegate *)[[UIApplication sharedApplication] delegate];
**self.managedObjectContext= = appDelegate.managedObjectContext;**
}