Ios Xcode:从URL获取字典密钥无效
我正在使用最新版本的Ios Xcode:从URL获取字典密钥无效,ios,swift,xcode,dictionary,Ios,Swift,Xcode,Dictionary,我正在使用最新版本的Xcode和Swift 我有以下代码来获取WKWebView的当前URL: let htmlURL = navigationAction.request.url!.absoluteURL print(htmlURL) print功能精确输出以下内容:https://www.example.com/ 现在,我有以下代码: if let htmlSourceCode = dict[htmlURL] { print(htmlSourceCode) } else {
Xcode
和Swift
我有以下代码来获取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")
}
出于某种原因,这给了我:找不到键
但是当直接使用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 {
}
使用
URL
的absoluteString
作为dict
的键。试试这个:
if let htmlSourceCode = dict[htmlURL.absoluteString] {
print(htmlSourceCode)
} else {
print("key not found")
}
什么是dict?请添加它。这意味着您正在验证您的dict作为密钥,您使用dict的值进行验证,将您的dict添加到此处您在dict中得到了什么?打印并粘贴到这里,这样我们就可以看到里面有什么值了。不客气。您应该添加有问题的
dict
声明。我犯了一个类似的错误,所以我可以认识到你的问题。