swift - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 '[String : Any]'

标签 swift swift3 alamofire optional-parameters

谁能告诉我如何解决这个问题?我只是想从事物说话中接收信号。

   `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] {

错误消息说“条件绑定(bind)的初始化程序必须具有可选类型,而不是'[String : Any]'”

最佳答案

如果你想使用条件绑定(bind),表达式的右边应该是可选的。

改变这个:

if let json = response.result.value as! [String:Any]

对此:

if let json = response.result.value as? [String:Any]

关于swift - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 '[String : Any]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828657/

相关文章:

ios - Firebase 消息传递中的 "Error : Use of undeclared type MessagingDelegate"

ios - 我是在添加另一个或更改此约束吗?

ios - 快速清空请求正文

ios - Swift 中的 ABAddressBook iOS 8 内存泄漏

swift - 相机不会跟随 Sprite

xcode - 在 Swift 中发出 Amazon Web Services S3 POST 请求

ios - Carthage 安装失败,代码为 65

swift - Info.plist 字典到枚举类型字典 :String

swift - 将 unicode 字符串截断为最大字节

php - 为什么我的 php json_encode 不能在 alamofire 中使用 .responseJSON 序列化?