条件绑定的初始值设定项必须具有 Optional 类型,而不是 '[String : Any]' [英] Initializer for conditional binding must have Optional type, not '[String : Any]'
本文介绍了条件绑定的初始值设定项必须具有 Optional 类型,而不是 '[String : Any]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我如何解决这个问题?我只是想从事物说话中接收信号.
Can anyone tell me how to fix this? Im just trying to receive signals from thing speak.
`self.title = "Home"
print("Requesting data...")
Alamofire.request( "https://api.thingspeak.com/channels/290427/feeds.json", parameters: ["results": "1", "location": "false"]) // Gets the latest info from ThingSpeak
.responseJSON { response in
print("Data downloaded: \(response.result)")
if let json = response.result.value as! [String:Any] {
print(json) //see full data
if let feeds = json["feeds"] as? [String: Any] {
for feed in feeds {
print(feed["field2"])
if let temperatureStr = feed["field2"] as? String, let dateStr = feed["created_at"] as? String {
if let temperature = Double(temperatureStr){
self.label.text = "Temperature: \(temperature)°F" //Displays last updated data entry
}
错误在一行
if let json = response.result.value as! [String:Any] {
错误消息说条件绑定的初始化程序必须具有可选类型,而不是 '[String : Any]'
Error message says "Initializer for conditional binding must have Optional type, not '[String : Any]'
推荐答案
如果你想使用条件绑定,表达式的右边应该是可选的.
If you wanna use conditional binding, the right side of the expression should be optional.
改变这个:
if let json = response.result.value as! [String:Any]
为此:
if let json = response.result.value as? [String:Any]
这篇关于条件绑定的初始值设定项必须具有 Optional 类型,而不是 '[String : Any]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文