将Core Data添加到现有项目后出错

时间:2014-09-03 18:17:30

标签: ios objective-c sqlite core-data

我已将CoreData添加到现有项目中。我在XCode中将LibData.framework添加到了Libs。 所以我的数据类是

import

@interface UBCoreDataStack : NSObject

+(instancetype) defaultStack;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

并实施

#import "UBCoreDataStack.h"

@implementation UBCoreDataStack

@synthesize managedObjectModel;
@synthesize managedObjectContext;
@synthesize persistentStoreCoordinator;

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

//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;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"UBFoodDeliveryModel" withExtension:@"momd"];
  //  managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"UBFoodDelivery.sqlite"] ;
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                  initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil URL:storeURL options:nil error:&error]) {
        /*Error for store creation should be handled in here*/
    }

    return persistentStoreCoordinator;
}

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSURL *)applicationStoresDirectory {


    NSURL *storesDirectory =
    [[NSURL fileURLWithPath:[self applicationDocumentsDirectory]]
     URLByAppendingPathComponent:@"Stores"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:[storesDirectory path]]) {
        NSError *error = nil;
        if ([fileManager createDirectoryAtURL:storesDirectory
                  withIntermediateDirectories:YES
                                   attributes:nil
                                        error:&error]) {

        }
        else {NSLog(@"FAILED to create Stores directory: %@", error);}
    }
    return storesDirectory;
}

+(instancetype) defaultStack{
    static UBCoreDataStack * defaultStack;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defaultStack = [[self alloc] init];
    });

    return defaultStack;
}

@end

但是现在当我尝试用数据库做一些操作时,我总是在方法中出错   NSURL * storeURL = [[self applicationDocumentsDirectory] ​​URLByAppendingPathComponent:@" UBFoodDelivery.sqlite"];

The error is 
2014-09-03 22:35:15.663 UBFoodDelivery[1793:60b] -[NSPathStore2 URLByAppendingPathComponent:]: unrecognized selector sent to instance 0x9da6a10
2014-09-03 22:35:15.753 UBFoodDelivery[1793:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPathStore2 URLByAppendingPathComponent:]: unrecognized selector sent to instance 0x9da6a10'

可能是因为它无法找到sqlite?但是我该如何添加呢?

1 个答案:

答案 0 :(得分:0)

您的applicationDocumentsDirectory会返回NSString类型。要通过附加路径组件来创建URL,您必须先转换为NSURL

NSURL *directoryURL = [NSURL fileURLWithPath:[self applicationDocumentsDirectory]];
NSURL *storesDirectory = [directoryURL ...