我通过以下代码选择文件:
- (IBAction)selectFile:(id)sender {
// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setPrompt:@"Select"];
fileTypes = [NSArray arrayWithObjects:@"wmv", @"3gp", @"mp4", @"avi", @"mp3", @"mma", @"wav", @"jpeg", @"png", @"jpg", @"tiff", nil];
// NSArray *JpegfileTypes = [NSArray arrayWithObjects:@"jpeg", @"png", @"jpg", @"tiff", @"mp3" nil];
// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];
//Enable multiple selection of files
[openDlg setAllowsMultipleSelection: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 types:fileTypes] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
files = [[openDlg filenames] retain];
int i; // Loop counter.
// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString *tempFilePath = [files objectAtIndex:i];
NSLog(@"tempFilePath::: %@",tempFilePath);
inputFilePath = [[files objectAtIndex:i] retain];
NSLog(@"filename::: %@", inputFilePath);
// Do something with the filename.
[selectedFile setStringValue:inputFilePath];
NSLog(@"selectedFile:::: %@", selectedFile);
}
}
}
然后在选择之后我使用此代码处理所选文件。
- (IBAction)setMessage:(id)sender {
[fileGenProgress startAnimation:self];
NSString *message = [[NSString alloc] initWithFormat:@"Started"];
[lblMessage setStringValue:message];
[message release];
[self startProcessingVideoFile];
[self startProcessingAudioFile];
[self startProcessingJpg];
}
我面临的问题是,我没有得到如何比较不同的字符串,如果所选文件是3gp / mp4或jpg或mp3。好像用户选择了一些视频文件,然后方法[self startProcessingVideoFile];
将运行,如果他选择了一些JPG或PNG等文件,那么[self startProcessingAudioFile];
方法将运行。
所选择的路径不仅仅是文件的扩展名。因此,在这种情况下,如何强制- (IBAction)setMessage:(id)sender
方法运行适当的方法。
答案 0 :(得分:0)
您可以像这样获得字符串的扩展名(格式):
NSString *extension = [stringPath pathExtension];
现在进行比较,当您知道文件的扩展名时,这很容易。 例如:
NSLog(extension);
if ([extension isEqualToString:@"3gp"] || [ext isEqualToString:@"mp4"]) {
[self startProcessingVideoFile];
}
等等。
<强>更新强>
您的IBAction:SetMessage
应如下所示:
- (IBAction)setMessage:(id)sender {
[fileGenProgress startAnimation:self];
NSString *message = [[NSString alloc] initWithFormat:@"Started"];
[lblMessage setStringValue:message];
[message release];
NSString *extension = [inputFilePath pathExtension];
NSLog(extension);
if ([extension isEqualToString:@"3gp"] || [ext isEqualToString:@"mp4"]) {
[self startProcessingVideoFile];
}
// And etc for others formats.
//[self startProcessingAudioFile];
//[self startProcessingJpg];
}