NSURL 到 NSString 转换做奇怪的事情
我正在尝试将 NSURL
转换为 NSString
以在程序中的其他位置使用。
这是我的代码:
- (IBAction)openExistingDocument:(id)sender {
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:NO];
[panel setAllowsMultipleSelection:NO];
[panel setMessage:@"Please select a File or Folder containing your character's .png layers"];
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
NSURL* theDoc = [[panel URLs] objectAtIndex:0];
NSString* unformattedURL = [NSString stringWithContentsOfURL:theDoc encoding:NSASCIIStringEncoding error:NULL];
NSString * formattedURL = [unformattedURL stringByReplacingOccurrencesOfString:@"file:/" withString:@""];
NSLog(@"the url says:%@", theDoc);
NSLog(@"the unformatted string says: %@", unformattedURL);
NSLog(@"the formatted string says: %@", formattedURL);
}
}];
}
当我运行程序时,这是我的命令行输出:
2012-01-29 18:43:19.205 Cocos2dCharacterRigger[516:407] the url says:file://localhost/Users/*****/Desktop/mockupsite.jpg
2012-01-29 18:43:19.213 Cocos2dCharacterRigger[516:407] the unformatted string says: ÿØÿá.]Exif
2012-01-29 18:43:19.219 Cocos2dCharacterRigger[516:407] the formatted string says: ÿØÿá.]Exif
有人能指出我做错了什么吗?
I'm trying to convert a NSURL
to a NSString
to use elsewhere in my program.
Here's my code:
- (IBAction)openExistingDocument:(id)sender {
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:NO];
[panel setAllowsMultipleSelection:NO];
[panel setMessage:@"Please select a File or Folder containing your character's .png layers"];
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
NSURL* theDoc = [[panel URLs] objectAtIndex:0];
NSString* unformattedURL = [NSString stringWithContentsOfURL:theDoc encoding:NSASCIIStringEncoding error:NULL];
NSString * formattedURL = [unformattedURL stringByReplacingOccurrencesOfString:@"file:/" withString:@""];
NSLog(@"the url says:%@", theDoc);
NSLog(@"the unformatted string says: %@", unformattedURL);
NSLog(@"the formatted string says: %@", formattedURL);
}
}];
}
When I run the program, here's what my command line outputs:
2012-01-29 18:43:19.205 Cocos2dCharacterRigger[516:407] the url says:file://localhost/Users/*****/Desktop/mockupsite.jpg
2012-01-29 18:43:19.213 Cocos2dCharacterRigger[516:407] the unformatted string says: ÿØÿá.]Exif
2012-01-29 18:43:19.219 Cocos2dCharacterRigger[516:407] the formatted string says: ÿØÿá.]Exif
Can someone point out what I'm doing incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将 URL 处的文件内容加载到字符串中。您将文件作为 ASCII 字符串加载(该文件几乎肯定不是——它是图像),并且忽略任何错误。
要获取 url 的实际路径,请向 url 发送
-path
消息。因此,要获取带有路径的字符串:这应该打印以下内容(基于上面的日志):
You are loading the contents of the file at the URL into the string. You are loading the file as an ASCII string (which that file almost certainly is not -- it's an image) and you are ignoring any errors.
To get the actual path for the url, you send the url the
-path
message. So, to get a string with the path:This should print the following (based on your logs above):