1

ユーザーにディレクトリを要求する Mac OS X アプリケーションを作成しようとしています。ユーザーが「参照」ボタンを押したときにトリガーされる NSOpenPanel を使用しています。

問題は、[NSOpenPanel filenames] が廃止されたため、現在は URL 関数を使用していることです。通常のファイルパスを取得するだけのURLに関連するものを解析したいと思います。だから私は試しfileName = [fileName stringByReplacingOccurrencesOfString:@"%%20" withString:@" "];ましたが、それは私にエラーを与えました:

-[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x100521fa0

メソッド全体は次のとおりです。

- (void) browse:(id)sender
{
    int i; // Loop counter.

    // Create the File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:NO];

    // Enable the selection of directories in the dialog.
    [openDlg setCanChooseDirectories:YES];

    // Display the dialog.  If the OK button was pressed,
    // process the files.
    if ( [openDlg runModal] == NSOKButton )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [openDlg URLs];

        // Loop through all the files and process them.
        for( i = 0; i < [files count]; i++ )
        {
            NSString* fileName = (NSString*)[files objectAtIndex:i];
            NSLog(@"%@", fileName);

            // Do something with the filename.
            fileName = [fileName stringByReplacingOccurrencesOfString:@"%%20" withString:@" "];

            NSLog(@"%@", fileName);
            NSLog(@"Foo");
            [oldJarLocation setStringValue:fileName];
            [self preparePopUpButton];
        }
    }
}

興味深いことに、「Foo」はコンソールに出力されません。メソッドが stringByReplacigOccurencesOfString 行で中止されるようなものです。

その 1 行を削除すると、アプリが実行され、テキスト ボックスに文字列が URL 形式で入力されますが、これは望ましくありません。

4

1 に答える 1

1

あなたの問題は、NSArray返されたオブジェクトではなく、オブジェクト[NSOpenPanel URLs]が含まれていることです。次のキャストを行っています。NSURLNSString

 NSString* fileName = (NSString*)[files objectAtIndex:i];

NSArrayは を返すため、idキャストが意味を成していることを確認するためのコンパイル時のチェックはありませんが、NSString実際に であるものにセレクターを送信しようとすると実行時エラーが発生しますNSURL

オブジェクトを変換してコードをほとんどそのまま使用できますが、URL のデコードを自分で処理する必要はありません。パーセントエンコーディングも元に戻すパス部分を取得するためのメソッドがすでにあります。NSURLNSStringNSURLpath

NSString *filePath = [yourUrl path];

あなたのコードパーセントでエンコードされた だけを扱っていたとしてもNSString、 theres stringByReplacingPercentEscapesUsingEncoding:.

于 2011-10-10T01:59:39.073 に答える