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

Posted

技术标签:

【中文标题】Swift 3 - \'Any?\' 类型的值没有成员“valueForKey”【英文标题】:Swift 3 - Value of type 'Any?' has no member 'valueForKey'Swift 3 - 'Any?' 类型的值没有成员“valueForKey” 【发布时间】:2016-09-14 12:24:02 【问题描述】:

您好,我已更新到 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

【问题讨论】:

您能否告诉我们,您如何定义result 以及它是什么类型? 您可能会将result 转换为比未指定的Any 更有意义的东西 Since id now imports as Any rather than AnyObject, you may see errors where you were previously performing dynamic lookup on AnyObject. in here ctrl+f -> any 谢谢!编辑后的帖子有答案! 【参考方案1】:

虽然您找到了答案,但最好使用纯 swift 方法而不是使用 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"] 

【讨论】:

以上是关于Swift 3 - 'Any?' 类型的值没有成员“valueForKey”的主要内容,如果未能解决你的问题,请参考以下文章

“任何”的值类型没有成员 'objectforKey' Swift 3 转换

Swift 3“任何”类型的值没有成员“valueForKey”

类型'Any'没有下标成员Swift 3

Swift 3 - 类型'Any'没有下标成员

类型“Any”没有下标成员/Swift 3 [重复]

类型 Any 没有下标成员 Swift 3.0