2013-04-17 166 views
0

enter image description here無法從資源文件夾中的文件複製到文檔文件夾

這裏是我的代碼:

- (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文件夾始終是空的,我怎麼能解決這個問題?謝謝。

編輯:

enter image description here

+0

嘗試NSLog的。 .. –

+0

@AnoopVaidya它不是,但我不知道爲什麼。 – jxdwinter

+0

現在打印defaultFilePath並檢查文件是否存在於此路徑中。 –

回答

1

你肯定BQWDatabase.db被包含在構建?您可以通過在XCode中打開右側窗格,選擇頂部最左側的圖標,然後展開'目標成員'來檢查。

+0

看看我的編輯。 – jxdwinter

0

我做我自己的代碼非常類似的東西,只是我覺得默認的文件如下:

NSString *defaultFilePath = [[[NSBundle mainBundle] pathForResource:@"BQWDatabase" ofType:@"db"]; 

不能確定它是否讓成功裏面如果copyItem後的差異

相關問題