我有一个正在修改的工作项目,
它在尝试使用 coredata 后崩溃,
项目之前没有coredata,所以加了,
就像以前的基础 sdk [number??] 一样,applicationDocumentsDirectory
返回一个 NSString,而不是 URL,
我已经把它改成工作了??但在尝试使用 coredata 时崩溃,
#import "QRGenAppDelegate.h"
//#import "QRGenViewController.h"
#import "MainViewController.h"
@implementation QRGenAppDelegate
//@synthesize window; //viewController
@synthesize window = _window;
@synthesize navigationController = _navigationController;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (void)dealloc
{
[__managedObjectModel release];
[__managedObjectContext release];
[__persistentStoreCoordinator release];
[_window release];
[_navigationController release];
//[viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
MainViewController *menuVC= [[[MainViewController alloc]init]autorelease];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:menuVC];
//self.window.rootViewController = menuVC;
self.navigationController.navigationBar.hidden = YES;
[self.window addSubview:self.navigationController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//CD
//Explicitly write Core Data accessors
- (NSManagedObjectContext *) managedObjectContext {
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
__managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
//mi solucto!!
//NSURL *storeURL= [NSURL URLWithString:[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ClientOcci.sqlite"]];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"db.sqlite" ofType:nil];
NSURL *storeURL = [NSURL URLWithString:filePath];
// NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ClientOcci.sqlite"];
//NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ChildCare_v02.sqlite"]; //actual SDK style for blank db
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
+ (NSString*)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
//CD
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
它崩溃了:
__managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
EXC_BAD_ACCESS
所以我如何在项目设置中尝试制作基本 SDK 5.1 但仍然崩溃,
为什么我的项目不喜欢 URL 作为返回方法?如何解决?如果我将其更改为较新的 SDK 会以其他方法制动吗?
编辑>>我已经尝试过@ierror的建议,
添加到我的rootVC:
if (managedObjectContext == nil) {
managedObjectContext = [(QRGenAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);}
但是现在在appDelegate中崩溃了,
在
- (NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
__managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; // crashes here!!!
return __managedObjectModel;
}
该怎么办?
多谢!