2010-08-04 3 views
9

내 응용 프로그램에서 사용자가 사운드 클립을 녹음하도록하고 나중에 사용자가 선택한 경우 삭제할 수있게하려고합니다.iPhone/Objective C : 파일을 삭제할 수 없습니다.

Error: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x391b7f0 "Operation could not be completed. (Cocoa error 4.)"

경로가있다,

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSLog(@"File exists: %d", [fileManager fileExistsAtPath:path]); 
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]); 
[fileManager removeItemAtPath:path error:&error]; 
if (error != nil) 
{ 
    NSLog(@"Error: %@", error); 
    NSLog(@"Path to file: %@", path); 
} 

문제는 fileExistsAtPathisDeletableFileAtPath 수익을 널 (null)과 removeItemAtPath이 작동하지 않는다는 것입니다,이 오류가 발생합니다 :

내가 사용하는 코드입니다 이 형식 :

/Users/andrei/Library/Application%20Support/iPhone%20Simulator/User/Applications/5472B318-FA57-4F8D-AD91-7E06E9609215/Documents/1280913694.caf 

1280913694.caf이라고하는 파일이 있지만 가져 오지 않습니다. 경로가 표현되어야하는 방식과 관련이 있습니까?

AVAudioPlayer으로 오디오 파일을 재생할 때 경로가 작동합니다.

는 또한 fileExistsAtPathisDeletableFileAtPath에 대한 %d%@을 변경 한 대답은 내가 FALSE 의미 가정 0이다.

파일의 이름이 데이터베이스에 저장되고, 파일의 경로는이 방법으로 검색됩니다 :이 값을 얻을 후

-(NSString *)returnFullPathToDirectory 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 

, 나는 다음과 같은 코드에서 사용

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
+1

'-removeItemAtPath'를'% d' (으)로 변경하고 다시 실행하십시오. – kennytm

+0

file : // 접두사는 NSURL로 변환하고 .path 속성을 얻는 것과 같이 제거해야합니다. (실제로 경로가 위에 있음) – ThomasRS

답변

26

수표 올바르지 않습니다. 메서드의 반환 값으로 BOOL을 설정하고 메서드를 성공적으로 완료 할 수 있고 나중에 오류가 없으면 오류 조건을 처리하는 데 사용해야합니다. 따라서 파일이 실제로 삭제되었을 수 있지만 잘못된 오류가 다시 발생합니다.

파일이 없으면 삭제하지 마십시오. 즉,이 코드는 (경로가 다른 곳에서 정의 된) 내 프로젝트에서 작동

을 쉽게 읽을 수로

또한, 나는 대개 오류의 localizedDescription 로그 :

NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    BOOL fileExists = [fileManager fileExistsAtPath:path]; 
    NSLog(@"Path to file: %@", path);   
    NSLog(@"File exists: %d", fileExists); 
    NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]); 
    if (fileExists) 
    { 
     BOOL success = [fileManager removeItemAtPath:path error:&error]; 
     if (!success) NSLog(@"Error: %@", [error localizedDescription]); 
    } 

관련 대답 : NSError: Does using nil to detect Error actually turn off error reporting?

0

URL 인코딩 된 파일 경로가 의심 스럽습니다. 나는 어딘가에 아이폰 파일 경로가 요즘 URL로 표현되어야한다고 읽었지 만, 공간을 URL 인코딩해야한다고 생각하지 않습니다.

경로에서 % 20을 (를) 제거하고 다시 시도해보십시오.

+1

파일 경로를 다음과 같이 지정합니다. **/Users/andrei/Library/응용 프로그램 지원/iPhone 시뮬레이터/사용자/응용 프로그램/0547AE74-AD10-4BEF-98CF-8FFF5BB2F70F/문서/1280913694.caf ** 그래서 % 20은 아니지만 ... 지금 이상한 ... 그것은 파일 존재하지 않고 (fileExistsAtPath에서 0을 반환 함) 파일이 삭제 가능하다는 것을 의미합니다 (isDeletableFileAtPath에서 1을 반환). 오류가 계속 발생하고 파일을 삭제하지 않습니다. – Andrei

+0

경로를 검색하는 데 사용하는 코드를 게시 해 주시겠습니까? – Jasarien

+0

두 번째 편집 (** edit2 **) 원래 질문에 게시 – Andrei

-8

는에

NSFileManager *fileManager = [NSFileManager defaultManager]; 

변경하려고 : (! 오류 = 무기 호)에 대한

NSFileManager *fileManager = [NSFileManager new]; 
+4

어, 아니요. 그는 시작하는 것이 옳았습니다. –

+0

그것도 적절한 목표입니까? – Jason

관련 문제