ユーザーにディレクトリを要求する 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 形式で入力されますが、これは望ましくありません。