Вызов методов после сравнения строковых объектов из NSArray Cocoa - PullRequest
0 голосов
/ 20 марта 2012

Я выбираю файлы с помощью этого кода:

- (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 запустить соответствующий метод.

1 Ответ

0 голосов
/ 20 марта 2012

Вы можете получить расширение (формат) строки следующим образом:

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];

}

...