stringByReplacingOccurenceOfString 失败

发布于 2024-12-08 23:46:48 字数 1754 浏览 6 评论 0原文

我正在尝试制作一个 Mac OS X 应用程序,要求用户提供目录。我正在使用 NSOpenPanel,当用户按下“浏览”按钮时会触发它。

问题是,[NSOpenPanel filenames] 已被弃用,所以现在我正在使用 URL 函数。我想解析出与 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 形式,这是我不想要的。

I'm trying to make a Mac OS X application that asks the user for a directory. I'm using an NSOpenPanel that gets triggered when the user presses a "Browse" button.

The problem is, [NSOpenPanel filenames] was deprecated, so now I'm using the URLs function. I want to parse out the stuff that's url related to just get a normal file path. So I tried fileName = [fileName stringByReplacingOccurrencesOfString:@"%%20" withString:@" "];, but that gave me an error:

-[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x100521fa0

Here's the entire method:

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

Interestingly enough, "Foo" never gets outputted to that console. It's like the method aborts at the stringByReplacigOccurencesOfString line.

If I remove that one line, the app will run and fill my text box with the string, just in URL form, which I don't want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-12-15 23:46:48

您的问题是 [NSOpenPanel URLs] 返回的 NSArray 包含 NSURL 对象,而不是 NSString 对象。您正在执行以下转换:

 NSString* fileName = (NSString*)[files objectAtIndex:i];

由于 NSArray 返回一个 id,因此没有任何编译时检查来确保您的转换有意义,但您确实得到了当您尝试将 NSString 选择器发送到实际上是 NSURL 时,会出现运行时错误。

可以NSURL对象转换为NSString并按原样使用您的代码,但您无需自己处理URL解码。 NSURL 已经有一个检索路径部分的方法,该方法也撤消百分比编码:路径

NSString *filePath = [yourUrl path];

即使您的代码仅处理百分比编码的NSString,也存在stringByReplacingPercentEscapesUsingEncoding:

Your problem is that the NSArray returned by [NSOpenPanel URLs] contains NSURL objects, not NSString objects. You're doing the following cast:

 NSString* fileName = (NSString*)[files objectAtIndex:i];

Since NSArray returns an id, there isn't any compile-time checking to make sure your cast makes sense, but you do get a runtime error when you try to send an NSString selector to what is actually an NSURL.

You could convert the NSURL objects to NSString and use your code mostly as-is, but there's no need for you to handle the URL decoding yourself. NSURL already has a method for retrieving the path portion which also undoes percent-encoding: path.

NSString *filePath = [yourUrl path];

Even if your code was dealing with just a percent-encoded NSString, theres stringByReplacingPercentEscapesUsingEncoding:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文