下标会员的 Facebook SDK 错误

Facebook SDK error for subscript members

我正在使用 Facebook SDK 登录并在我这边获取数据,但无法解决我面临的与语法相关的挑战

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
        }

    })
}

在行 var email = user["email"] as ? String 中,我收到任何类型的错误?没有成绩单。

试试这个:

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
    }
}

您需要将 user 类型 Any 转换为类型 Dictionary [String:AnyObject],然后您才可以使用下标从中提取值。