objective-c URLWithString: 返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981390/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:40:37  来源:igfitidea点击:

URLWithString: returns nil

iphoneobjective-curlnsstringnsurl

提问by gcamp

it may be very easy, but I don't seems to find out why is URLWithString:returning nil here.

这可能很容易,但我似乎不明白为什么URLWithString:在这里返回 nil。

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false&key=", webName];
NSURL* url = [NSURL URLWithString:stringURL];

回答by gerry3

You need to escape the non-ASCII characters in your hardcoded URL as well:

您还需要转义硬编码 URL 中的非 ASCII 字符:

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

You can probably remove the escaping of the localisationName since it will be handled by the escaping of the whole string.

您可能可以删除 localisationName 的转义,因为它将通过整个字符串的转义来处理。

回答by Islam.Ibrahim

Use This Function if you deal with file saved on file manager.

如果您处理保存在文件管理器中的文件,请使用此功能。

NSURL *_url = [NSURL fileURLWithPath:path];

回答by Yuji

I guess you need to use -[NSString stringByAddingPercentEscapesUsingEncoding:]. See Apple doc.

我想你需要使用-[NSString stringByAddingPercentEscapesUsingEncoding:]. 请参阅Apple 文档

Another comment is that, as an old timer, I find it a bit uneasy to put non-ASCII characters in a source file. That said, this Apple docsays, starting from 10.4, UTF-16 strings are OK inside @"...". Somehow GCC seems to correctly convert the source file in Latin-1 into UTF-16 in the binary, but I think it's safest to use 7-bit ASCII characters only inside the source code, and use NSLocalizedString.

另一个评论是,作为一个老手,我发现将非 ASCII 字符放在源文件中有点不安。也就是说,这个Apple 文档说,从 10.4 开始,UTF-16 字符串在@"...". 不知何故,GCC 似乎正确地将 Latin-1 中的源文件转换为二进制中的 UTF-16,但我认为最安全的是仅在源代码中使用 7 位 ASCII 字符,并使用NSLocalizedString.

回答by Ben Gottlieb

I think your accented characters are throwing things off; they won't be handled by -stringByAddingPercentEscapesUsingEncoding:.

我认为你的重音字符正在扔东西;它们不会被 -stringByAddingPercentEscapesUsingEncoding: 处理。

回答by Ryan Heitner

NSURL URLWithString:@"" will return nil if the URL does not conform to RFC 2396 and they must be escaped

如果 URL 不符合 RFC 2396,NSURL URLWithString:@"" 将返回 nil 并且它们必须被转义

If you read through rfc2396in the link you will get loads of details

如果您通读链接中的rfc2396,您将获得大量详细信息

A great site I found for checking where the offending character is, choose the path option for URL

我发现一个很棒的网站,用于检查违规字符的位置,选择 URL 的路径选项

http://www.websitedev.de/temp/rfc2396-check.html.gz

http://www.websitedev.de/temp/rfc2396-check.html.gz

回答by Iggy

The URLWithString: call will return a nil if the string passed to it is malformed. Since NSURL returns nil for malformed urls, NSLog your string and set breakpoints to see exactly what is being passed to your NSURL creation method. If your URLWithString works with a hard coded value, that's further proof that whatever you are passing is malformed.see

如果传递给它的字符串格式错误,则 URLWithString: 调用将返回 nil。由于 NSURL 为格式错误的 url 返回 nil,因此 NSLog 您的字符串并设置断点以准确查看传递给您的 NSURL 创建方法的内容。如果您的 URLWithString 使用硬编码值,则进一步证明您传递的任何内容都是格式错误的。

回答by fady

You can use NSURL directly without NSString.

你可以不用 NSString 直接使用 NSURL。

//.h file

//.h文件

@interface NewsBrowser : UIViewController {

    UIWebView *webView;
    NSURL *NewsUrl;

}

@property (nonatomic, retain) IBOutlet UIWebView *webView;

@property(nonatomic,assign)NSURL *NewsUrl;

@end

//.m file

//.m 文件

[webView loadRequest:[NSURLRequest requestWithURL:NewsUrl]];

And I pass the URL from another view (using NewsUrl variable) to this view.

我将 URL 从另一个视图(使用 NewsUrl 变量)传递到这个视图。

Try it.

尝试一下。