6

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.
    }
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Vyshakh
  • 632
  • 1
  • 11
  • 29
  • This might help: http://stackoverflow.com/questions/39516199/type-any-has-no-subscript-members-in-xcode-8-swift-3/39516303#39516303 – Bista Sep 21 '16 at 09:25

2 Answers2

12

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
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
10

For Swift 3.

change this-->

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

Into -->

if let fbemail = (result as AnyObject)["email"]! as? String
{
   print(fbemail)
}
juanjo
  • 3,737
  • 3
  • 39
  • 44
srivas
  • 299
  • 4
  • 10
  • 4
    @Fogmeister, that didn't work for me, so i posted my answer because it may help some one, posting an answer doesn't harm anyone, they're is no point giving a down mark, through out stackoverflow you'll find many answers rather than the accepted one. what is your problem giving a downmark.? – srivas Apr 19 '17 at 07:13