Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Any?' has no subscript members [duplicate]

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.
    }
}
like image 374
Vyshakh Avatar asked Sep 21 '16 09:09

Vyshakh


2 Answers

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
    }
like image 65
Anbu.Karthik Avatar answered Sep 28 '22 14:09

Anbu.Karthik


For Swift 3.

change this-->

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

Into -->

if let fbemail = (result as AnyObject)["email"]! as? String
{
   print(fbemail)
}
like image 24
srivas Avatar answered Sep 28 '22 15:09

srivas