2012-09-10 28 views
2

我想從數據包複製數據庫文件到用戶文檔。無法複製數據庫文件使用copyItemAtPath與可可錯誤4

我的代碼如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *userPath = [documentsDir stringByAppendingPathComponent:@"db.sql"]; 

NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSLog(@"Bundle database exists: %i",[fileManager fileExistsAtPath:srcPath]); 
NSLog(@"User Document folder database exists: %i",[fileManager fileExistsAtPath:userPath]); 


BOOL find = [fileManager fileExistsAtPath:userPath]; 
BOOL copySuccess = FALSE; 

NSError *error; 

if (!find) { 
    NSLog(@"don't have writable copy, need to create one"); 
    copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error]; 
} 

if (!copySuccess) { 

    NSLog(@"Failed with message: '%@'.",[error localizedDescription]); 
} 

,結果總是說:

捆綁數據庫中存在:1個用戶文檔文件夾數據庫中存在:0
沒有寫副本,需要創建一個Failed with消息:'
操作無法完成。 (可可錯誤4)'。

請建議,謝謝。

回答

2

確定用戶文檔目錄的代碼不正確。

使用你的代碼,我把一個快速和骯髒的示例工作。對於您的應用程序,您可能希望創建一些包含靜態函數applicationDocumentsDirectory的utils類,以便項目中的其他類可以根據需要調用它。

頭文件:

#import <UIKit/UIKit.h> 

@interface TST_ViewController : UIViewController 

+ (NSString *) applicationDocumentsDirectory; 

@end 

實現文件:

#import "TST_ViewController.h" 

@interface TST_ViewController() 

@end 

@implementation TST_ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSString *dbFilename = @"db.sql"; 
    NSString *userPath = [[TST_ViewController applicationDocumentsDirectory] stringByAppendingPathComponent:dbFilename]; 
    NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFilename]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    BOOL foundInBundle = [fileManager fileExistsAtPath:srcPath]; 
    BOOL foundInUserPath = [fileManager fileExistsAtPath:userPath]; 
    BOOL copySuccess = FALSE; 

    NSError *error; 

    if(foundInBundle) { 

     if (!foundInUserPath) { 
      NSLog(@"Don't have a writable copy, so need to create one..."); 
      copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error]; 
     } 

     if (!copySuccess) { 
      NSLog(@"Failed with message: '%@'.",[error localizedDescription]); 
     } 

    } else { 
     // handle error in the event the file is not included in the bundle 
    } 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

+ (NSString *) applicationDocumentsDirectory 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    return basePath; 
} 

@end 
+1

我改變的NSArray *路徑= NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask,YES); 至 NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 和一切都很好。謝謝。 – vic

相關問題