Vyshakh Vyshakh - 2 months ago 39
Swift Question

Type 'Any?' has no subscript members fbsdkgraphrequest,swift3

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.

The error is:

Type 'Any?' has no subscript members
.

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.
}
}

Answer

change this

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

into

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

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

full answer

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
    }