![](/img/trans.png)
[英]How do I check in Swift if a variable with a datatype of URL is empty?
[英]How do I express an empty URL in Swift 3?
我有一个iOS
项目。 我正在使用Xcode 8
和现在的Swift 3
。 在开始将项目从Swift 2
转换为最新版本之前,我在AppDelegate
具有以下内容:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
// Transfer incoming file to global variable to be read
if url != "" {
// Load from Mail App
incomingFileTransfer = url
incomingStatus = "Incoming"
} else {
// Regular Load
print("App Delegate: No incoming file")
incomingFileTransfer = nil
}
return true
}
效果很好,它会查看应用程序是否打开了传入的PDF类型文件。 我有与我的应用程序关联的PDF文件,正在我的应用程序中打开它们。
我使用了迁移工具来帮助将代码转换为新的Swift 3
代码。
它在AppDelegate
转换了我的代码,并将其更改为:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
// Transfer incoming file to global variable to be read
if url != "" {
// Load from Mail App
incomingFileTransfer = url
incomingStatus = "Incoming"
} else {
// Regular Load
print("App Delegate: No incoming file")
incomingFileTransfer = nil
}
return true
}
但是它给出了错误:
二进制运算符'!='不能应用于类型'URL'和'String'的操作数
我知道原来是NSURL
的“开放网址”现在是类型URL
。 我现在如何在Xcode 8和Swift 3中表示一个空URL? 我在网上看了,找不到任何相关信息?
感谢您的帮助和建议。
尝试使用URL
absoluteString
属性进行比较是否为空。
if url.absoluteString != "" {
}
else {
}
注意:如果您只想检查是否为空,则在您的情况下使用String
isEmpty
属性, if !url.absoluteString.isEmpty {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.