1

这是我所做的:使用填充版本的应用程序创建了 sqlite 文件。将该文件复制到我的应用程序包中,确保目标已设置并且它存在于“复制包资源”中

然后我尝试像这样填充核心数据:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{  

if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestModel.sqlite"];


if( ![[NSFileManager defaultManager]
      fileExistsAtPath:[storeURL path]] ) {
    // If there’s no Data Store present (which is the case when the app first launches), identify the sqlite file we added in the Bundle Resources, copy it into the Documents directory, and make it the Data Store.
    NSString *sqlitePath = [[NSBundle mainBundle]
                            pathForResource:@"TestModel" ofType:@"sqlite"
                            inDirectory:nil];
    NSError *anyError = nil;
    BOOL success = [[NSFileManager defaultManager]
                    copyItemAtPath:sqlitePath toPath:[storeURL path] error:&anyError];

    if(success){
        NSLog(@"sucess, loading into store");
        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();
        }else{
            NSLog(@"error with sqlite file");
        }
    }
}

return _persistentStoreCoordinator;

}

但我得到这个错误:

NSFileManager copyItemAtPath:toPath:error:]: source path is nil

我究竟做错了什么?是说在捆绑包中找不到sqlite文件吗?

4

2 回答 2

1

我认为这是因为当我将文件从 Finder 导入 Xcode 时,它​​被命名为“TestModel”而不是“TestModel.sqlite”。

于 2012-09-07T00:00:33.707 回答
0

从文档

bundlePath -- 顶级捆绑目录的路径。这必须是有效路径。例如,要为 Mac 应用程序指定捆绑目录,您可以指定路径 /Applications/MyApp.app。

*编辑:我想我用错了方法:(,不过还是试试这个!->为什么不使用- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension?跳过目录(因为你不想要一个)。

于 2012-09-06T23:47:26.460 回答