gpt4 book ai didi

ios - Xcode:从 URL 获取字典键不起作用

转载 作者:行者123 更新时间:2023-12-01 19:31:32 26 4
gpt4 key购买 nike

我正在使用最新版本的 XcodeSwift .
我有以下代码来获取 WKWebView 的当前 URL :

let htmlURL = navigationAction.request.url!.absoluteURL
print(htmlURL)
print函数输出如下: https://www.example.com/现在,我有以下代码:
if let htmlSourceCode = dict[htmlURL] {
print(htmlSourceCode)
} else {
print("key not found")
}
出于某种原因,这给了我: key not found但是直接使用时 https://www.example.com/而不是 htmlURL像这样:
if let htmlSourceCode = dict["https://www.example.com/"] {
print(htmlSourceCode)
} else {
print("key not found")
}
它正在工作并按预期向我显示控制台中的源代码。
为什么它与 dict["https://www.example.com/"] 一起使用但不是 dict[htmlURL]虽然在这两种情况下是完全相同的字符串吗?
编辑: dict如下:
if let path = Bundle.main.path(forResource: "html", ofType: "plist") {
let dictRoot = NSDictionary(contentsOfFile: path)
if let dict = dictRoot {
}

最佳答案

使用 URLabsoluteString作为 dict的 key 。试试这个:

if let htmlSourceCode = dict[htmlURL.absoluteString] {
print(htmlSourceCode)
} else {
print("key not found")
}

关于ios - Xcode:从 URL 获取字典键不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62510171/

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