Facebook SDK error for subscript members [duplicat

2019-09-21 09:36发布

This question already has an answer here:

I am using Facebook SDK for login and fetching data at my end but unable to solve the issue I am facing challenges related to syntax

func fetchProfile() {
    let parameters = ["fields": "email, first_name, last_name, picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).start(completionHandler: { (connection, user, requestError) -> Void in

        if requestError != nil {
            print(requestError)
            return
        }

        var email = user["email"] as? String
        let firstName = user["first_name"] as? String
        let lastName = user["last_name"] as? String

        self.nameLabel.text = "\(firstName!) \(lastName!)"

        var pictureUrl = ""

        if let picture = user["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String {
            pictureUrl = url
        }

    })
}

In the line var email = user["email"] as ? String i getting error as type any? has no transcript.

1条回答
Rolldiameter
2楼-- · 2019-09-21 10:02

Try this:

if let dataDict = user as? [String:AnyObject] {
    var email     = dataDict["email"] as? String
    let firstName = dataDict["first_name"] as? String
    let lastName  = dataDict["last_name"] as? String

    //self.nameLabel.text = "\(firstName!) \(lastName!)"

    var pictureUrl = ""

    if let picture = dataDict["picture"] as? [String:AnyObject], let data = picture["data"] as? [String:AnyObject], let url = data["url"] as? String {
        pictureUrl = url
    }
}

You need to convert the user type Any to type Dictionary [String:AnyObject], then only you can use subscripts to extract value from it.

查看更多
登录 后发表回答