【问题标题】:Calling methods after comparing string objects from NSArray Cocoa比较来自 NSArray Cocoa 的字符串对象后调用方法
【发布时间】:2023-03-23 02:30:01
【问题描述】:

我正在通过此代码选择文件:

- (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 方法运行适当的方法。

【问题讨论】:

    标签: objective-c xcode cocoa xcode4.2


    【解决方案1】:

    你可以像这样得到字符串的扩展名(格式):

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

    }

    【讨论】:

    • 感谢 Justin Boo,但 NSString *extension = [string pathExtension];不被接受,在哪里使用它?
    • @iError 字符串是您的文件路径。在您的代码中,我看到“inputFilePath”。看更新。
    猜你喜欢
    • 2010-10-27
    • 2013-02-10
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多