NSURL 到 NSString 转换做奇怪的事情

发布于 2024-12-31 21:54:45 字数 1344 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

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

发布评论

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

评论(1

诗笺 2025-01-07 21:54:45

您正在将 URL 处的文件内容加载到字符串中。您将文件作为 ASCII 字符串加载(该文件几乎肯定不是——它是图像),并且忽略任何错误。

要获取 url 的实际路径,请向 url 发送 -path 消息。因此,要获取带有路径的字符串:

NSString* filePath = [theDoc path];
NSLog(@"the url: %@", theDoc);
NSLog(@"the path: %@", filePath);

这应该打印以下内容(基于上面的日志):

the url: file://localhost/Users/*****/Desktop/mockupsite.jpg
the file: /Users/*****/Desktop/mockupsite.jpg

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:

NSString* filePath = [theDoc path];
NSLog(@"the url: %@", theDoc);
NSLog(@"the path: %@", filePath);

This should print the following (based on your logs above):

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