0
回答
Objective-C 的 stringByReplacingOccurenceOfString 失败
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   

在开发一个 Mac 应用时,需要用户选择一个目录,因此使用了 NSOpenPanel 。

问题是 [NSOpenPanel filenames] 已经被废弃了,所以我用了 URLs 函数。我想通过解析这个url来获取文件路径,就用了

fileName = [fileName stringByReplacingOccurrencesOfString:@"%%20" withString:@" "];

但报了如下错误:
-[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x100521fa0

下面是整个方法的代码:
- (void) browse:(id)sender
{
    int i; // Loop counter.

    // Create the File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:NO];

    // 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 runModal] == NSOKButton )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [openDlg URLs];

        // Loop through all the files and process them.
        for( i = 0; i < [files count]; i++ )
        {
            NSString* fileName = (NSString*)[files objectAtIndex:i];
            NSLog(@"%@", fileName);

            // Do something with the filename.
            fileName = [fileName stringByReplacingOccurrencesOfString:@"%%20" withString:@" "];

            NSLog(@"%@", fileName);
            NSLog(@"Foo");
            [oldJarLocation setStringValue:fileName];
            [self preparePopUpButton];
        }
    }
}

有趣的是 "Foo" 没有输出到控制台,似乎程序在 stringByReplacigOccurencesOfString  这行终止了。

如果我删除这行,应用执行就没问题,但只是 URL 格式的结果。

举报
无鸯
发帖于7年前 0回/736阅
顶部