2011-08-30 4 views
10

내 응용 프로그램 번들에서 내 응용 프로그램의 문서 디렉토리로 파일을 복사하려고합니다. "Cocoa Error 262"오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?여기에 내 사본에 문제가 있습니까?

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"]; 
NSURL *initialURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]]; 

NSError *error = nil; 

if (![[NSFileManager defaultManager] fileExistsAtPath:[initialURL absoluteString]]) { 
    NSLog(@"Original does not exist. \nPath: %@", [initialURL absoluteString]); 
} 

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL absoluteString]]) { 
    NSLog(@"Destination file does not exist. \nPath: %@", [storeURL absoluteString]); 

    [[NSFileManager defaultManager] copyItemAtURL:initialURL toURL:storeURL error:&error]; 

    NSLog(@"Error: %@", [error description]); 
} 

답변

32

일반 오래된 파일 경로로 URL을 초기화하는 중입니다.

NSURL *initialURL = 
    [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" 
                 ofType:@"sqlite"]]; 

[NSURL fileURLWithPath:]을 대신 사용하십시오.

3

당신이 점점 오류가 나는 당신의 경로 중 하나가 제대로 형성되지 않은 의미 생각

NSFileReadUnsupportedSchemeError Read error because the specified URL scheme is unsupported

입니다 : 여기에 내 코드입니다. 아마도 이러한 경로를 로그에 기록하고 예상대로 로그가 나오는지 확인하십시오.

+0

올바른 계획은 무엇이며 어디에서이 정보를 얻었습니까? – Moshe

+0

전체 "file : /// path/to/file"경로를 빌드하지 않으려면 + URLWithString :을 사용하지 마십시오. 그러나 당신이 + fileURLWithPath 일 때 당신은 왜 그렇게 할 것입니까? – kperryua

+0

[link] (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html)에서 정보를 얻었습니다. 올바른 구성표는 URL이 얼마나 중요한지를 의미합니다. 형식이 지정됩니다. – smitec

1

오류 262는 FoundationErrors.h에서 NSFileReadUnsupportedSchemeError로 정의됩니다.

NSLog()를 사용하여 사용중인 두 URL의 리터럴 값을 작성하고 file : // URL이고 완성 된 것으로 확인하는 것이 좋습니다.

+0

두 값이 모두 완료되었습니다. – Moshe

+0

그리고 그들은 file : // URLs입니까? –

2

솔직히 말해서 문제가 무엇인지 잘 모르겠지만 문제를 해결했습니다. 나는 다시 작동 코드를 통해 이동해야하지만, 여기있다 :

NSError *error = nil; 


NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", @"CoreData", @"sqlite"]]; 

//if file does not exist in document directory, gets original from mainbundle and copies it to documents. 

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
    NSString *defaultFilePath = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]; 
    [[NSFileManager defaultManager] copyItemAtPath:defaultFilePath toPath:filePath error:&error]; 

    if (error != nil) { 
     NSLog(@"Error: %@", error); 
    } 
} 

편집 :

내가 응용 프로그램 디렉토리에 대한 경로가 주어진 잘못된 것으로 의심 그 applicationDocumentsDirectory 생성의 몸 위에 표시된 documentsDorectory 변수의 값과 다른 방법으로 표시됩니다.

관련 문제