// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
// 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 runModalForDirectory:nil file:nil] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];
NSString *string1;
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++)
{
NSString* fileName = [files objectAtIndex:i];
// Do something with the filename.
string1 = [[NSString alloc] initWithContentsOfFile:fileName];
if(string1 == nil)
{
NSLog(@"Error reading file");
}
else
{
}
}
}
当我第一次单击打开按钮时,它将打开打开对话框,并且可以正常工作
但是当我第二次点击打开按钮时,它给出了一个错误
*-[NSCFArray objectAtIndex:]:超出范围(0)的索引(0)
最佳答案
不确定是否能解决您的问题,但是不建议使用runModalForDirectory,请考虑使用runModal。
关于cocoa - 尝试访问超出数组范围的索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7369776/