gpt4 book ai didi

ios - 如何在 Swift 3 中表达空 URL?

转载 作者:行者123 更新时间:2023-12-02 04:53:20 30 4
gpt4 key购买 nike

我有一个 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
}

但是它给出了错误:

Binary operator '!=' cannot be applied to operands of type 'URL' and 'String'

据我了解,之前的“开放网址”是一个 NSURL,现在是一个 URL 类型。现在如何在 Xcode 8 和 Swift 3 中表达空 URL?我上网查了一下,没有找到任何相关信息?

感谢您的帮助和建议。

最佳答案

尝试使用URLabsoluteString属性来比较它是否为空。

if url.absoluteString != "" {

}
else {

}

注意:如果您只想检查是否为空,请在您的情况下使用 StringisEmpty 属性,它看起来像if !url.absoluteString.isEmpty {

关于ios - 如何在 Swift 3 中表达空 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39856987/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com