2011-11-12 13 views
7

SOLVED(ありがとうRegexident)ファイル名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]; 
    } 
} 

したがって、名前にスペースがない文書がある場合は、しかし、ファイル名に空白がある場合、ファイル名は存在しないと言います。そして、if-elseメソッドを削除すると、initが発生しますが、ファイルが存在しないためにクラッシュします。私はファイルパスの%20を通常のスペースに置き換えようとしましたが、メソッドはelseパーツを呼び出し続けます。

ファイルのパスは標準化されていないのですか?

+0

"%20"と変数ネーミング( "... URL")がコード内にあると判断すると、ファイルパス(スペースを "%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

関連する問題