objective-c 将文件路径从 NSString 转换为 NSURL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting File Path From NSString To NSURL
提问by spamguy
I'm working through Cocoa smoothly, but this problem seems so basic it cancels out all the cool stuff I learned. :/
我正在顺利地处理 Cocoa,但是这个问题似乎很基本,它抵消了我学到的所有很酷的东西。:/
I have a generated file path, and it needs to be in NSURL format. From research, this is the code I wrote:
我有一个生成的文件路径,它需要是 NSURL 格式。经过研究,这是我写的代码:
NSLog(@"Old path = %@", pathToFile);
NSURL *xmlURL = [[[NSURL alloc] init] fileURLWithPath:pathToFile];
NSLog(@"New path = %@", [xmlURL absoluteString]);
And the output:
和输出:
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] Old path = file://localhost/Users/[username]/Music/iTunes/iTunes%20Music%20Library.xml
2010-01-27 15:39:22.105 MusicLibraryStats[28574:a0f] New path = (null)
First off, the alloc-init shouldn't even be necessary; other people seem to get away with it. In this case, if I don't alloc-init, I get an 'unrecognized selector' error on that line. Of course, now I'm just getting plain old (null).
首先, alloc-init 甚至不需要;其他人似乎逍遥法外。在这种情况下,如果我不使用 alloc-init,我会在该行上收到“无法识别的选择器”错误。当然,现在我只是变老了(空)。
Where did I goof?
我哪里瞎了?
Thanks!
谢谢!
回答by zneak
The [[NSURL alloc] init]is not just unnecessary, it's invalid. fileURLWithPath:is a class method, which means you can only call it on the class object (that is, NSURLitself). It does not produce a compile error because -(NSURL *)initreturns an object of type id, and does not result in a runtime error because -(NSURL *)initactually returns nil, and messages sent to nilwill just cascade another nilas their return value.
这[[NSURL alloc] init]不仅是不必要的,而且是无效的。fileURLWithPath:是一个类方法,这意味着你只能在类对象(即它NSURL本身)上调用它。它不会产生编译错误,因为-(NSURL *)init返回类型为 的对象id,并且不会导致运行时错误,因为-(NSURL *)init实际上返回nil,并且发送到的消息nil只会级联另一个nil作为它们的返回值。
This code should work:
此代码应该工作:
NSString* pathToFile = @"/this/is/a/path";
NSURL* url = [NSURL fileURLWithPath:pathToFile];
NSString* pathToFile = @"/this/is/a/path";
NSURL* url = [NSURL fileURLWithPath:pathToFile];
I found your problem.-[NSOpenPanel URLs]returns an array of NSURL objects, which you treat as NSString objects. That's not right. You should use the following:
我发现了你的问题。-[NSOpenPanel URLs]返回一个 NSURL 对象数组,您将其视为 NSString 对象。那是不对的。您应该使用以下内容:
NSURL* url = [[oPanel URLs] objectAtIndex:0];
The debugger could've show you that if you looked at the pathToFilevariable. Make sure to check it next time. :) Hovering a variable with your mouse should get you its type.
如果您查看pathToFile变量,调试器可能会告诉您。下次一定要检查一下。:) 用鼠标悬停一个变量应该会得到它的类型。
However, remember that there are situations where you will legitimately encounter another type than the one you expected. For instance, the private NSPathStore2 class is part of the NSString cluster, and you can do everything NSString supports on NSPathStore2 objects. (If this happens and you're not too sure, check the documentation to see if the type you expect is a cluster type. That's how they're called in the documentation.)
但是,请记住,在某些情况下,您会合法地遇到不同于您预期的类型。例如,私有 NSPathStore2 类是 NSString 集群的一部分,您可以在 NSPathStore2 对象上执行 NSString 支持的所有操作。(如果发生这种情况并且您不太确定,请检查文档以查看您期望的类型是否为集群类型。文档中就是这样调用它们的。)

