NSURL URLWithString: raises exception

Posted by Stephen Darlington on Stack Overflow See other posts from Stack Overflow or by Stephen Darlington
Published on 2009-05-17T18:29:15Z Indexed on 2010/03/15 19:19 UTC
Read the original article Hit count: 674

Filed under:
|
|

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.

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];

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

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

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.

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about crash-reports