2014-03-05 3 views
2

json 파일을 응용 프로그램 번들에서 Document 디렉토리로 복사하려하지만 놀랍게도 그렇게 할 수 없습니다.도큐멘트 디렉토리에 쓸 수 없음

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) 
    { 
     NSString *themesPath = [[NSBundle mainBundle] pathForResource:@"themes" ofType:@"json"]; 
     NSData *data  = [NSData dataWithContentsOfFile:themesPath]; 

     BOOL success = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; 

     if (!success) 
      NSLog(@"Fail"); 
    } 

그러나 그것을 : 내가 this question을 발견하고 다음과 같이 내 코드를 수정하려고했습니다 검색 후

Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x194a0e90 {NSSourceFilePathErrorKey=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSUserStringVariant=( Copy ), NSFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json, NSDestinationFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json, NSUnderlyingError=0x194a0400 "The operation couldn’t be completed. Operation not permitted"}

:

NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docDir = [array lastObject]; 
NSString *path = [docDir stringByAppendingPathExtension:@"themes.json"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) 
    { 
    NSString *themesPath = [[NSBundle mainBundle] pathForResource:@"themes" ofType:@"json"]; 
    NSError *error  = nil; 

    [[NSFileManager defaultManager] copyItemAtPath:themesPath toPath:path error:&error]; 

    if (error) 
     NSLog(@"Error %@", error); 
    } 

그것은 다음과 같은 오류가 발생합니다 : 여기 코드는 어느 쪽도 작동하지 않습니다. success 변수는 NO입니다. 내가 시도한 마지막 것은 :

[data writeToFile:path atomically:YES]; 

하지만 여전히 헛된 것입니다. NO을 반환합니다.

이 문제는 장치에서만 발생합니다. 시뮬레이터에서는 괜찮습니다. 아무도 내게 단서를 줄 수 있습니까? 이것은 우리가 오류 메시지를 읽을 분명해진다

[docDir stringByAppendingPathComponent:@"themes.json"]; 

, 당신이 그것을에 파일을 쓰려고 볼 :

[docDir stringByAppendingPathExtension:@"themes.json"]; 

가 있어야한다 :

답변

4

첫 번째 예는 한 가지에 올 /var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json. .이있는 곳에 /이 있어야합니다.

stringByAppendingPathExtension: 파일, jpg, txt, html에 확장을 추가하는 데 사용됩니다 .....

stringByAppendingPathComponent

, 당신이 경우 올바른 디렉토리 구분 기호를 추가하여 경로에 파일을 추가합니다 / .

두 번째 예제는 앱 번들이 읽기 전용이므로 확실히 실패합니다.

+2

당신은 동일한 잘못된 문자열을 두 번 반복 복사했습니다. – Vladimir

+0

@AndreyChernukha 죄송합니다. 과거의 오류를 유감스럽게 생각하며 두 가지 방법의 차이점에 대한 설명을 추가했습니다. – rckoenes

+0

@rckoenes 실제로 작동했습니다. 고마워. 4 분 안에 답을 수락하겠습니다. –

관련 문제