2012-09-10 2 views
2

번들에서 사용자 문서로 데이터베이스 파일을 복사하려고합니다.코코아 오류가있는 copyItemAtPath를 사용하여 데이터베이스 파일을 복사 할 수 없습니다.

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 message : '
작업을 완료 할 수 없습니다. (코코아 오류 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

I는 (YES, NSDocumentationDirectory, NSUserDomainMask) NSArray를 경로 * = NSSearchPathForDirectoriesInDomains의 변화; ~ NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 및 모든 것이 좋습니다. 감사. – vic

관련 문제