Cocoaでファイルオープンボックスを開く方法を調べていたら、このページにたどり着きました。OS X 10.7 のリリースにより、リンクされている多くのサンプルが非推奨になりました。そのため、コンパイラの警告を回避するためのサンプル コードを次に示します。
// -----------------
// NSOpenPanel: Displaying a File Open Dialog in OS X 10.7
// -----------------
// Any ole method
- (void)someMethod {
// Create a File Open Dialog class.
NSOpenPanel *openDlg = [NSOpenPanel openPanel];
// Set array of file types
NSArray<NSString*> *fileTypesArray = @[@"jpg", @"gif", @"png"];
// Enable options in the dialog.
[openDlg setCanChooseFiles:YES];
[openDlg setAllowedFileTypes:fileTypesArray];
[openDlg setAllowsMultipleSelection:YES];
// Display the dialog box. If OK is pressed, process the files.
if ([openDlg runModal] == NSModalResponseOK) {
// Get list of all files selected
NSArray<NSURL*> *files = [openDlg URLs];
// Loop through the files and process them.
for (NSURL *file in files) {
// Do something with the filename.
NSLog(@"File path: %@", [file path]);
}
}
}