我有一个文件event_setup.sqlite
,该文件放在我的应用中并复制到Build Phases中的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