gpt4 book ai didi

iOS - 将 sqlite 复制到 CoreData bundle

转载 作者:行者123 更新时间:2023-11-29 13:29:03 25 4
gpt4 key购买 nike

这是我所做的:使用应用程序的填充版本创建 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

我做错了什么?是说在bundle中找不到sqlite文件吗?

最佳答案

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

关于iOS - 将 sqlite 复制到 CoreData bundle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12309828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com