2011-11-12 2 views
7

SOLVED (감사의 말)파일 이름 NSString은 공간에 불필요한 % 20을 추가합니다.

PDF의 파일 경로를 사용자 정의 -(id)init init 메소드에 전달하는 앱이 있습니다. 테이블에 추가되고 선택되면 존재하지 않는 파일에 대해 else 문을 호출합니다.

- (void) gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index { 

NSLog (@"Selected theArgument=%d\n", index); 

UIViewController *viewController = [[[UIViewController alloc]init]autorelease]; 
{ 
    //if file is built-in, read from the bundle 
    if (index <= 3) 
    { 
     // first section is our build-in documents 
     NSString *fileURLs = [_documentIconsURLs objectAtIndex:index]; 
     NSLog(@"file url -%@", fileURLs); 
     viewController = [[[xSheetMusicViewController alloc]initWithContentURL:fileURLs]autorelease]; 
    } 
    //if file is external, read from URL 
    else 
    { 
     // second section is the contents of the Documents folder 
     NSString *fileURL = [_documentIconsURLs objectAtIndex:index]; 
     NSLog(@"file url -%@", fileURL); 
     NSString *path; 
     NSString *documentsDirectoryPath = [self applicationDocumentsDirectory]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileURL]; 


     if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryPath]) 
     { 
      viewController = [[[xSheetMusicViewController alloc]initWithDocumentURL:fileURL]autorelease]; 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure!"               message:@"The Selected File Does Not Exist" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles: nil]; 
      [alert show]; 
      [alert release];   
      return; 
     } 
    } 
[self.navigationController setNavigationBarHidden:YES animated:NO]; 
[UIView beginAnimations:@"animation" context:nil]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:YES]; 
[self.navigationController pushViewController:viewController animated:NO]; 
[UIView commitAnimations]; 
    } 
} 

그래서 이름에 공백이없는 문서가있을 때마다 푸시되고 inits됩니다. 그러나 파일 이름에 공백이 있으면 파일이 존재하지 않는다고 말합니다. 그리고 if-else 메서드를 제거하면 init이 발생하지만 파일이 없기 때문에 충돌이 발생합니다. 파일 경로의 % 20을 일반 공백으로 바꾸려고 시도했지만이 메서드는 else 부분을 계속 호출합니다.

파일 경로가 표준화되고 읽을 수 없습니까, 아니면 다른 방법이 잘못 되었습니까?

+0

코드에서 "% 20"과 변수 이름 ("... URL")을 판단하면 파일 URL ("% 20"등으로 공백을 "20"등으로 인코딩)을 혼란스럽게합니다 (공백, 기타). – Regexident

+0

파일 url은 documentInteraction 예제에서 apple 메소드로 문자열로 변환됩니다. URL은 비 -DI 컨트롤러로 전달됩니다. 그래서 어떻게 고쳐야합니까? – CodaFi

답변

16

경로는 비율이 나는이 시도 것 경로 문자열을 이스케이프하기 위해 다음과 같이 나타납니다

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

그리고 나는 그것이 URL 경로를 포함하는있는 NSString 아닌 있다고 명확하게 제시하는 fileURLStringfileURL의 이름을 바꿀 것 실제 NSURL (귀하의 변수 이름이 잘못 암시하는).

하지만 문제의 원인은 probabaly이기 때문에 _documentIconsURLs이 채워지는 코드를 확인해 보겠습니다.

+0

좋은 시도! 그러나 당신의 이전 제안은 옳았습니다. [filename path] 대신 [filename lastpathcomponent]를 호출했습니다. 나는 여전히 당신을 투표 할 것입니다. 고맙습니다. – CodaFi

관련 문제