7

解決済み (Regexident に感謝)

PDF のファイル パスをカスタム-(id)initinit メソッドに渡すアプリがあります。テーブルに追加され、選択されると、存在しないファイルに対して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 メソッドを削除すると、初期化されますが、ファイルが存在しないためクラッシュします。ファイル パスの %20 を通常のスペースに置き換えようとしましたが、メソッドは引き続き else 部分を呼び出します。

では、ファイル パスが標準化されておらず、読み取り可能ではないのでしょうか、それとも私の else メソッドが間違っているのでしょうか?

4

1 に答える 1

16

あなたのパスはパーセンテージエスケープされたパス文字列のように見えるので、私はこれを試してみます:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

そして、実際の NSURL ではなく、URL パスを含む NSString であることを明確にするために、名前fileURLをに変更します (変数名が誤って暗示します)。fileURLString

_documentIconsURLsしかし、おそらく問題の原因であるため、入力するコードを確認します。

于 2011-11-12T20:37:02.857 に答える