我不想添加一个sqlite文件来为我的应用程序预加载数据。我创建了这个文件并将其复制到项目目录中。我将它添加到Copy Bundle Resources并复制文件。但是,当我不想使用这个文件时,我得到一个错误,说没有这样的文件。如果有人能帮助我,我将不胜感激。 代码如下:
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CookBookData" ofType:@"sqlite"]] ;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]) {
NSLog(@"Couldn't preload data, error: %@", error);
}
错误:
2015-07-29 15:42:34.342 CookBook v1 [2897:24912]无法预加载数据,错误:错误Domain = NSCocoaErrorDomain Code = 4“操作无法完成。(Cocoa错误4.) “UserInfo = 0x7fe4a35275d0 {NSSourceFilePathErrorKey = / Users / user / Library / Developer / CoreSimulator / Devices /.../ data / Containers / Bundle / Application /.../ CookBook v1.app/CookBookData.sqlite,NSUserStringVariant =( 复制 ),NSDestinationFilePath = / Users / user / Library / Developer / CoreSimulator / Devices /.../ Documents / CookBookModel 2.sqlite,NSFilePath = / Users / user / Library / Developer / CoreSimulator / Devices /.../ data / Containers /Bundle/Application/.../CookBook v1.app/CookBookData.sqlite,NSUnderlyingError = 0x7fe4a35145d0“操作无法完成。没有这样的文件或目录”}
我试图更改我的代码:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CookBookModel3.sqlite"];
NSError *error = nil;
NSString *storePath = [storeURL path];
NSLog(@"Error %@", error);
NSString *dbName = @"CookBookData.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingString:dbName];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (success) {
NSLog(@"Success");
}
else{
NSLog(@"Error %@", [error localizedDescription]);
}
}
else {
NSLog(@"Already exists");
}
if (![[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:storePath error:&error]) {
NSLog(@"Couldn't preload data, error: %@", error);
}
但我得到另一个错误:
2015-07-30 12:12:29.118 CookBook [29209:208802]无法预加载数据,错误:错误Domain = NSCocoaErrorDomain Code = 516“操作无法完成。(Cocoa error 516.)” UserInfo = 0x7fe9526318b0 {NSSourceFilePathErrorKey = / Users / user / Library / Developer / CoreSimulator / Devices / DA5D105A-C9AF-4C91-80B3-DFF2C157CC92 / data / Containers / Data / Application / BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985 / Library / DocumentationCookBookData .sqlite,NSUserStringVariant =( 复制 ),NSDestinationFilePath = / Users / user / Library / Developer / CoreSimulator / Devices / DA5D105A-C9AF-4C91-80B3-DFF2C157CC92 / data / Containers / Data / Application / BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985 / Documents / CookBookModel3.sqlite ,NSFilePath = / Users / user / Library / Developer / CoreSimulator / Devices / DA5D105A-C9AF-4C91-80B3-DFF2C157CC92 / data / Containers / Data / Application / BEED2AB9-BFDA-40AC-A8D0-55DD81F9D985 / Library / DocumentationCookBookData.sqlite, NSUnderlyingError = 0x7fe952610c30“操作无法完成。文件存在”} 2015-07-30 12:12:29.121 CookBook [29209:208802]托管对象上下文
另一种变体:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"CookBookData.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:path toPath:storePath error:&error];
答案 0 :(得分:2)
使用以下内容获取相关文件的路径:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"CookBookData.sqlite"];
然后,您可以使用以下内容复制文件:
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
[fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
我会包含比上面更多的检查和验证 - 特别是NSFileManager中的fileExistsAtPath:
方法。
答案 1 :(得分:1)
我希望,这会有所帮助:
NSError *error;
NSString *dbName = @"CookBookData.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingString:dbName];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
if ([fileManager fileExistsAtPath:defaultDBPath]) { //check if file exists in NSBundle
BOOL success = [fileManager fileExistsAtPath:dbPath]; // check if exists in document directory
if (!success) {
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; //copy to document directory
if (success) {
NSLog(@"Database successfully copied to document directory");
}
else{
NSLog(@"Error %@", [error localizedDescription]);
}
}
else {
NSLog(@"Already exists in document directory");
}
}
else{
NSLog(@"Does not exists in NSBundle");
}
您可以检查两件事,转到您的应用程序文档目录,检查是否可以去那里,其他事情是确保您的sqlite文件已正确添加到项目中,您可以删除并再次添加。让我知道,如果还有问题