stringByReplacingOccurenceOfString 失败
我正在尝试制作一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
[NSOpenPanel URLs]
返回的NSArray
包含NSURL
对象,而不是NSString
对象。您正在执行以下转换:由于
NSArray
返回一个id
,因此没有任何编译时检查来确保您的转换有意义,但您确实得到了当您尝试将NSString
选择器发送到实际上是NSURL
时,会出现运行时错误。您可以将
NSURL
对象转换为NSString
并按原样使用您的代码,但您无需自己处理URL解码。NSURL
已经有一个检索路径部分的方法,该方法也撤消百分比编码:路径
。即使您的代码仅处理百分比编码的
NSString
,也存在stringByReplacingPercentEscapesUsingEncoding:
。Your problem is that the
NSArray
returned by[NSOpenPanel URLs]
containsNSURL
objects, notNSString
objects. You're doing the following cast:Since
NSArray
returns anid
, 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 anNSString
selector to what is actually anNSURL
.You could convert the
NSURL
objects toNSString
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
.Even if your code was dealing with just a percent-encoded
NSString
, theresstringByReplacingPercentEscapesUsingEncoding:
.