這裏是我的代碼:
- (void)copyFileIfNeeded
{
NSFileManager *fileManager= [NSFileManager defaultManager];
NSError *error;
NSString *filePath = [self getFile];
BOOL success = [fileManager fileExistsAtPath:filePath];
if (!success)
{
NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"BQWDatabase.db"];
success = [fileManager copyItemAtPath:defaultFilePath toPath:filePath error:&error];
}
}
- (NSString *)getFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"BQWDatabase.db"];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self copyFileIfNeeded];
return YES;
}
但Documents文件夾始終是空的,我怎麼能解決這個問題?謝謝。
編輯:
嘗試NSLog的。 .. –
@AnoopVaidya它不是,但我不知道爲什麼。 – jxdwinter
現在打印defaultFilePath並檢查文件是否存在於此路徑中。 –