我有一个文件event_setup.sqlite
放在我的应用程序中并复制到Copy Bundle Resources
构建阶段中的部分。
当将此文件从资源复制到文档目录时,我的应用程序说找不到该文件。我正在使用以下代码从资源中复制文件并告诉我该文件不存在:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self checkAndCreateDatabase: @"event_setup.sqlite"];
return YES;
}
-(void)checkAndCreateDatabase:(NSString *)name{
/*----Get the path to the Database----*/
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:name];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success){
NSLog(@"Database already exists");
return;
}
else{
NSLog(@"Database does not exist");
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
bool test = [fileManager fileExistsAtPath:databasePathFromApp];
if(test){
NSLog(@"file does exist in resources: %@", databasePathFromApp);
}
else{
NSLog(@"file does not exist in resources: %@", databasePathFromApp);
}
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
}
当我运行此代码时,我在日志中得到以下信息:
file does not exist in resources: /Users/samrowley/Library/Application Support/iPhone Simulator/6.1/Applications/583D4293-3AD5-45FB-B502-9919F51FDB7B/Test.app/event_setup.sqlite
我试过清理应用程序,重新启动 Xcode,删除文件并将其重新复制到 Xcode 中,但似乎没有任何效果。
编辑:
终端命令的输出:
-rw-r--r-- 1 samrowley staff 2797 11 Feb 10:51 divider.png
-rw-r--r-- 1 samrowley staff 2803 11 Feb 10:51 divider@2x.png
drwxr-xr-x 3 samrowley staff 102 11 Feb 10:51 en.lproj
-rw-r--r-- 1 samrowley staff 2937 11 Feb 10:51 header.png
-rw-r--r-- 1 samrowley staff 3205 11 Feb 10:51 header@2x.png