尝试从包复制到文档目录时出错
Error trying to copy from bundle to documents directory
我已将一个 sqlite 文件添加到我的应用程序中,并试图将其从包中复制到文档目录中。我也将 sqlite 添加到目标应用程序。以下是我用来复制文件的代码:
NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:destination]) {
NSString *source = [[NSBundle mainBundle] pathForResource:@"myapp" ofType:@"sqlite"];
NSError *error = nil;
[fileManager copyItemAtPath:source toPath:destination error:&error];
if (error) {
//
}
}
[Utils applicationDocumentsDirectory]
的代码:
+ (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
但是当执行这段代码时,我得到以下 error
:
Error Domain=NSCocoaErrorDomain Code=4 "The file “myapp.sqlite”
doesn’t exist."
UserInfo={NSSourceFilePathErrorKey=/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite,
NSUserStringVariant=(
Copy ), NSDestinationFilePath=file:///Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Data/Application/13B22E15-D00C-433C-9F02-014B1F73D183/Documents/myapp.sqlite,
NSFilePath=/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite,
NSUnderlyingError=0x7faffe1b5cf0 {Error Domain=NSPOSIXErrorDomain
Code=2 "No such file or directory"}}
我使用终端检查了以下路径,发现 sqlite 文件确实存在于应用程序包中:
/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite
但我仍然收到此错误。我尝试了一切。我什至清理了构建文件夹并重新安装了应用程序,它仍然没有用。这可能是什么问题?如何解决?
我使用下面的代码将我的 SQLite 数据库从应用程序包复制到文档目录。
注意 'Database' 是文件夹名称。 'datasource' 是我的数据库名称,它的扩展名是 'db'
-(void)saveDatabase{
//put bundle database to DocumentDirectory
NSLog(@"%@",[self databasePath]);
//Create Path For Destination
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path = [path stringByAppendingPathComponent:@"Database"];
BOOL isExists=[[NSFileManager defaultManager]fileExistsAtPath:path];
if(!isExists){
NSError* error;
BOOL isCreated = [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
if(isCreated){
path = [path stringByAppendingPathComponent:@"Datasource"];
path = [path stringByAppendingPathExtension:@"db"];
isExists = [[NSFileManager defaultManager]fileExistsAtPath:path];
if(!isExists){
NSError* dbError;
NSString* bundle = [[NSBundle mainBundle]pathForResource:@"Datasource" ofType:@"db"];
isCreated = [[NSFileManager defaultManager]copyItemAtPath:bundle toPath:path error:&dbError];
if(isCreated){
NSLog(@"DatabasePath %@",path);
}else{
NSLog(@"ErrorCopying DB file %@",dbError.localizedDescription);
}
}else{
}
}else{
NSLog(@"Error While Creating Database Directory:->%@",error.localizedDescription);
}
}
}
- (NSString*)databasePath{
NSString* path = [self documentDirectory];
path = [path stringByAppendingPathComponent:@"Database"];
path = [path stringByAppendingPathComponent:@"Datasource"];
path = [path stringByAppendingPathExtension:@"db"];
return path;
}
- (NSString*)documentDirectory{
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
确保您的文件在您使用的目标中的 Build Phases -> Copy Bundle Resources
中列出。尝试删除 Derived data
。如果仍然不起作用,请转至 Derived data
并使用 Finder 检查文件。
您的代码几乎是正确的。此行有两个缺陷:
NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];
absoluteString
的使用不正确。这将以 file://path/to/Documents/
的形式给出一个文件 URL。您需要使用 path
方法从 NSURL
.
中获取文件路径(不是文件 URL)
然后您需要将文件名正确附加到路径中。您需要使用 stringByAppendingPathComponent:
而不是 stringByAppendingString:
。
那一行应该是:
NSString *destination = [[[Utils applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"myapp.sqlite"];
我已将一个 sqlite 文件添加到我的应用程序中,并试图将其从包中复制到文档目录中。我也将 sqlite 添加到目标应用程序。以下是我用来复制文件的代码:
NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:destination]) {
NSString *source = [[NSBundle mainBundle] pathForResource:@"myapp" ofType:@"sqlite"];
NSError *error = nil;
[fileManager copyItemAtPath:source toPath:destination error:&error];
if (error) {
//
}
}
[Utils applicationDocumentsDirectory]
的代码:
+ (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
但是当执行这段代码时,我得到以下 error
:
Error Domain=NSCocoaErrorDomain Code=4 "The file “myapp.sqlite” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite, NSUserStringVariant=( Copy ), NSDestinationFilePath=file:///Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Data/Application/13B22E15-D00C-433C-9F02-014B1F73D183/Documents/myapp.sqlite, NSFilePath=/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite, NSUnderlyingError=0x7faffe1b5cf0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
我使用终端检查了以下路径,发现 sqlite 文件确实存在于应用程序包中:
/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite
但我仍然收到此错误。我尝试了一切。我什至清理了构建文件夹并重新安装了应用程序,它仍然没有用。这可能是什么问题?如何解决?
我使用下面的代码将我的 SQLite 数据库从应用程序包复制到文档目录。
注意 'Database' 是文件夹名称。 'datasource' 是我的数据库名称,它的扩展名是 'db'
-(void)saveDatabase{
//put bundle database to DocumentDirectory
NSLog(@"%@",[self databasePath]);
//Create Path For Destination
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path = [path stringByAppendingPathComponent:@"Database"];
BOOL isExists=[[NSFileManager defaultManager]fileExistsAtPath:path];
if(!isExists){
NSError* error;
BOOL isCreated = [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
if(isCreated){
path = [path stringByAppendingPathComponent:@"Datasource"];
path = [path stringByAppendingPathExtension:@"db"];
isExists = [[NSFileManager defaultManager]fileExistsAtPath:path];
if(!isExists){
NSError* dbError;
NSString* bundle = [[NSBundle mainBundle]pathForResource:@"Datasource" ofType:@"db"];
isCreated = [[NSFileManager defaultManager]copyItemAtPath:bundle toPath:path error:&dbError];
if(isCreated){
NSLog(@"DatabasePath %@",path);
}else{
NSLog(@"ErrorCopying DB file %@",dbError.localizedDescription);
}
}else{
}
}else{
NSLog(@"Error While Creating Database Directory:->%@",error.localizedDescription);
}
}
}
- (NSString*)databasePath{
NSString* path = [self documentDirectory];
path = [path stringByAppendingPathComponent:@"Database"];
path = [path stringByAppendingPathComponent:@"Datasource"];
path = [path stringByAppendingPathExtension:@"db"];
return path;
}
- (NSString*)documentDirectory{
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
确保您的文件在您使用的目标中的 Build Phases -> Copy Bundle Resources
中列出。尝试删除 Derived data
。如果仍然不起作用,请转至 Derived data
并使用 Finder 检查文件。
您的代码几乎是正确的。此行有两个缺陷:
NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];
absoluteString
的使用不正确。这将以 file://path/to/Documents/
的形式给出一个文件 URL。您需要使用 path
方法从 NSURL
.
然后您需要将文件名正确附加到路径中。您需要使用 stringByAppendingPathComponent:
而不是 stringByAppendingString:
。
那一行应该是:
NSString *destination = [[[Utils applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"myapp.sqlite"];