Swift 3 - “Any”类型的值没有会员'valueForKey'

时间:2016-09-14 12:24:02

标签: swift3

您好我已更新为swift 3并收到错误:

  

“任何?”类型的值没有会员'valueForKey'

以下代码行:

let userName : NSString? = result.valueForKey("name") as? NSString
let facebookID : NSString? = result.valueForKey("id") as? NSString
let firstName : NSString? = result.valueForKey("first_name") as? NSString
let lastName : NSString? = result.valueForKey("last_name") as? NSString
let email : NSString? = result.valueForKey("email") as? NSString

这些错误的解释是什么?我将如何修复它?

非常感谢!

编辑:

完整代码:

  if((FBSDKAccessToken.current()) != nil){
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
                if (error == nil){
                    
                    
                    let userName : NSString? = result.valueForKey("name") as? NSString
                    let facebookID : NSString? = result.valueForKey("id") as? NSString
                    let firstName : NSString? = result.valueForKey("first_name") as? NSString
                    let lastName : NSString? = result.valueForKey("last_name") as? NSString
                    let email : NSString? = result.valueForKey("email") as? NSString
                    

解答:

  FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection,  result, error) -> Void in
                if (error == nil){
                    
                    let data:[String:AnyObject] = result as! [String : AnyObject]
                    
                    
                    let userName : NSString? = data["name"]! as? NSString
                    let facebookID : NSString? = data["id"]! as? NSString
                    let firstName : NSString? = data["first_name"]! as? NSString
                    let lastName : NSString? = data["last_name"]! as? NSString
                    let email : NSString? = data["email"]! as? NSString

1 个答案:

答案 0 :(得分:3)

虽然你找到了答案,但最好使用纯粹的快速方法而不是使用NSString,最好不要强制解包选项

FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection,  result, error) -> Void in
            if (error == nil){
                  guard let data = result as? [String:Any] else { return }

                  let fbid = data["id"] 
                  let username = data["name"]  
                  let firstName = data["first_name"]