简体   繁体   English

如果数据类型为URL的变量为空,我如何检查Swift?

[英]How do I check in Swift if a variable with a datatype of URL is empty?

I'm trying to check if my url is nil or not since I have a background thread that is getting the url from my database. 我正在尝试检查我的网址是否为零,因为我有一个后台线程从我的数据库获取网址。

I tried doing this travelURL.absoluteString == "" and travelURL != nil but the problem still persists. 我试过这个travelURL.absoluteString == ""travelURL != nil但问题仍然存在。

I declared my variable as var travelURL: URL! 我将我的变量声明为var travelURL: URL!

You can use if let syntax: 您可以使用if let语法:

if let url = travelURL {
    // Here you can use url, it is not nil
} else {
   // Here url is nil 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM