gpt4 book ai didi

swift 。网址返回零

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:52 24 4
gpt4 key购买 nike

我试图在我的应用程序中打开一个网站,但由于某种原因,一行一直返回 nil,这是我的代码:

let url = URL(string: "http://en.wikipedia.org/wiki/\(element.Name)")!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}

第一行 (let url = URL...) 不断返回此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value.

我应该怎么做才能解决这个问题?

最佳答案

I think this will help. Your element.name may contain space between words so addingPercentEncoding make PercentEncoding string.

let txtAppend = (element.Name).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = "http://en.wikipedia.org/wiki/\(txtAppend!)"
let openUrl = NSURL(string: url)
if #available(iOS 10.0, *) {
UIApplication.shared.open(openUrl as! URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(openUrl as! URL)
}

关于 swift 。网址返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40368389/

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