本文介绍了输入'Any?'没有下标成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中登录fb后检索电子邮件ID。但是,当我尝试从结果中获取值时,我收到错误。

I am trying to retrieve email id after fb login in my app. However I am getting an error when i try to get the value from result.

错误是:输入'Any?'没有下标成员

func fetchProfile()
{
    print("Fetch Profile")
    let parameters = ["fields": "email, first_name, last_name,  picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).start { (connection, result, error) in

        let email = result["email"] as? String  //Type 'Any?' has no subscript members error occurs here.
    }
}


推荐答案

更改这个

 let email = result["email"] as? String

into

  guard let resultNew = result as? [String:Any]

 let email = resultNew["email"]  as! String

完整答案

let parameters = ["fields": "email, first_name, last_name,  picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).start { (connection, result, error) in

        guard let resultNew = result as? [String:Any]

       let email = resultNew["email"]  as! String
    }

这篇关于输入'Any?'没有下标成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 10:39