NSURL URLWithString:引发异常 [英] NSURL URLWithString: raises exception

查看:272
本文介绍了NSURL URLWithString:引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之, [NSURL URLWithString:] 似乎引发了异常。根据文档如果字符串格式错误,则返回nil。没有提到在任何情况下都引发例外情况。除此之外,我还在对URL进行编码并在将字符串转换为URL之前检查 nil

In short, [NSURL URLWithString:] appears to be raising an exception. According to the documentation "If the string was malformed, returns nil." There is no mention of an exception being raised under any circumstance. In addition to this, I am both encoding the URL and checking for nil before converting the string to a URL.

在转换URL之前,任何人都可以提供有关它可能是异常还是我应该做的其他错误检查的任何建议吗?

Can anyone offer any advice as to which exception it could be or what other error checking I should be doing before converting the URL?

如果您对详细信息,调用代码如下:

In case you're interested in the details, the calling code looks like this:

NSString* tmpText = [newUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (tmpText == nil) {
    // error handling
}
else {
    NSURL* tmpURL = [NSURL URLWithString:tmpText];

这是从iTunes Connect下载的崩溃报告中的一部分:

And this is a section from the crash report download from iTunes Connect:

8   libobjc.A.dylib                 0x300c1f84 objc_exception_throw
9   CoreFoundation                  0x3029a598 +[NSException raise:format:arguments:]
10  CoreFoundation                  0x3029a538 +[NSException raise:format:]
11  Foundation                      0x30696dde -[NSURL initWithString:relativeToURL:]
12  Foundation                      0x30696cd8 +[NSURL URLWithString:relativeToURL:]
13  Foundation                      0x30696cae +[NSURL URLWithString:]
14  Yummy                           0x000146ca -[DeliciousPostCell setUrl:] + 46

以某种坏格式出现,但实际上应该返回 nil 也不例外。

It seems that the URL was in a "bad" format somehow but that should really be returning a nil not an exception.

我从不看到我自己引发了异常,所以我无法使用XCode捕获代码并查看结果是什么发生。并且遇到问题的用户从未与我直接联系,因此我无法要求更多详细信息。

I have never seen the exception being raised myself so I can't use XCode to trap the code and see what's happening. And the user(s) that experienced the problem never contacted me directly so I can't ask for more details. Any suggestions greatly appreciated.

更新(14/7/2009):似乎像这样的hack,但是我在可疑线。我还提出了Radar错误报告(#7031551),建议代码应与文档匹配。

Update (14/7/2009): Seems like such a hack, but I added an exception block around the suspect line. I also raised a Radar bug report (#7031551) suggesting that the code should match the documentation.

推荐答案

我遇到过几次API会根据文档抛出异常。我的建议是(在您的实际代码中)确保tmpText确实不是nil(在这种情况下会引发异常,因为大多数期望NSStrings的API不会保存在那里)。之后,只需在其周围添加异常处理并将错误报告提交到bugreporter.apple.com即可。

I have encountered some occasions where API throws exceptions that shouldn't according to documentation. My suggestion would be to be to make sure (in your actual code) that tmpText really isn't nil (in that case an exception is thrown, as most apis that expect NSStrings are not nil save there). After that, just add exception handling around it and file a bugreport to bugreporter.apple.com .

这篇关于NSURL URLWithString:引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆