Facebook SDK error for subscript members [duplicate]

南楼画角 提交于 2019-12-25 19:04:49

问题


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:


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.



来源:https://stackoverflow.com/questions/39740485/facebook-sdk-error-for-subscript-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!