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"];
가 있어야한다 :
당신은 동일한 잘못된 문자열을 두 번 반복 복사했습니다. – Vladimir
@AndreyChernukha 죄송합니다. 과거의 오류를 유감스럽게 생각하며 두 가지 방법의 차이점에 대한 설명을 추가했습니다. – rckoenes
@rckoenes 실제로 작동했습니다. 고마워. 4 분 안에 답을 수락하겠습니다. –