简体   繁体   English

如何在Swift 3中表达一个空URL?

[英]How do I express an empty URL in Swift 3?

I have an iOS project. 我有一个iOS项目。 I'm working on using Xcode 8 and now Swift 3 . 我正在使用Xcode 8和现在的Swift 3 Before I started converting my project from Swift 2 to the latest, I had the following in my AppDelegate : 在开始将项目从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
}

Worked great and it would see if there was an incoming PDF type file being opened by the app. 效果很好,它会查看应用程序是否打开了传入的PDF类型文件。 I have PDF files associated with my app and being to open them within my app. 我有与我的应用程序关联的PDF文件,正在我的应用程序中打开它们。

I used the migration tool to help convert my code to the new Swift 3 code. 我使用了迁移工具来帮助将代码转换为新的Swift 3代码。

It converted my code in the AppDelegate and changed it to: 它在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
}

However it gives the error: 但是它给出了错误:

Binary operator '!=' cannot be applied to operands of type 'URL' and 'String' 二进制运算符'!='不能应用于类型'URL'和'String'的操作数

I understand the 'open url' that was an NSURL is now a type URL instead. 我知道原来是NSURL的“开放网址”现在是类型URL How do I express an empty URL in Xcode 8 and Swift 3 now? 我现在如何在Xcode 8和Swift 3中表示一个空URL? I looked online and cannot find anything about this? 我在网上看了,找不到任何相关信息?

Thank you for the help and advice. 感谢您的帮助和建议。

Try to use absoluteString property of URL to compare is it empty. 尝试使用URL absoluteString属性进行比较是否为空。

if url.absoluteString != "" {

}
else {

}

Note: If you just want to check for empty, then batter to use isEmpty property of String in your case it is look like if !url.absoluteString.isEmpty { 注意:如果您只想检查是否为空,则在您的情况下使用String isEmpty属性, if !url.absoluteString.isEmpty {

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

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