0

I was integrating Facebook Login SDK, in my app and I am new to this. I implemented it, but now I want to get the email. So, following the tutorial I created the same function as the tutorial showcased.

Here is the function:

func fetchProfile()
{
    print("Fetch Profile")

    let parameters: [String : Any] = ["fields" : "email, first_name, last_name, picture.type(large)"]

    FBSDKGraphRequest(graphPath: "me", parameters: parameters).start(completionHandler: {

        (connection, result, error) -> Void in

        if error != nil {

            print(error!)

            return
        }

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

            print(email)

        }   
    }
}

So, the programmer was using Swift 3 there and I am using Swift 4 here, so is it creating any issue? And, it says Type "Any" has no subscript members where if let email = is written. How to solve this?

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
Rob
  • 2,086
  • 18
  • 25
  • cast result to `[String:Any]`with an if let – Reinier Melian Jan 25 '18 at 09:50
  • Did you search for "Type 'Any' Has No Subscript Member"? Because it's simple and there are too much question on SO about that. `result` is a `Any` object. Why should you be able to do `["email"]` on it (ie subscribe). No one said the compiler that `result` was in fact a `Dictionary`. In other words, the compiler tells you: That method (or way to do it) doesn't exist for that kind of object. So cast it with a `if let`. – Larme Jan 25 '18 at 09:58

1 Answers1

1

cast result to [String:Any]

if let resultDict = result as? [String:Any]{
   if let email = resultDict["email"] as? String  {
            print(email)
        }
} 
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55