从空字符串解析时如何使 NSURL 变量为零? [英] How to make NSURL variable is nil when parsed from null String?
本文介绍了从空字符串解析时如何使 NSURL 变量为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 iOS 开发的新手.有时我的 API 服务器返回一个 nil
值.当我尝试创建 NSURL
实例时,这会引发错误.
I'm a newbie in iOS development. Sometimes my API server returns a nil
value. This is raising an error when I try to create a NSURL
instance.
这里是条件:
NSString *tmpURL = nil;
NSURL *url = [NSURL URLFromString:tmpURL];
在这种情况下,它会使应用程序崩溃.我只是想让 url
变量为零,而不是引发错误.
In this condition, it will make the app crash. I just want to make the url
variable nil, not raise an error.
推荐答案
怎么样:
NSString *tmpURL = nil;
NSURL *url = tmpUrl ? [NSURL URLFromString:tmpURL] : nil;
这篇关于从空字符串解析时如何使 NSURL 变量为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文