Type 'Any?' has no subscript members [duplicate]
Asked Answered
D

2

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.
    }
}
Diplomatist answered 21/9, 2016 at 9:19 Comment(1)
This might help: #39516699Mantel
W
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
    }
Wangle answered 21/9, 2016 at 9:25 Comment(7)
Xcode is suggestion to change the following line let email = resultNew["email"] to let email = resultNew?["email"] And the value in email is nil after doing so.Diplomatist
@Diplomatist - which lineWangle
Updated commentDiplomatist
what the result you get here resultNewWangle
I am getting nil for that alsoDiplomatist
@Diplomatist - in ths place use [String:[Any]] or use this [String:Any]Wangle
Let us continue this discussion in chat.Diplomatist
F
10

For Swift 3.

change this-->

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

Into -->

if let fbemail = (result as AnyObject)["email"]! as? String
{
   print(fbemail)
}
Foundling answered 18/4, 2017 at 8:59 Comment(1)
@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.?Foundling

© 2022 - 2024 — McMap. All rights reserved.