swift3 – Swift 3 – “Any”类型的值没有会员’valueForKey’

swift3 – Swift 3 – “Any”类型的值没有会员’valueForKey’,第1张

概述嗨我已更新到 swift 3并且收到错误: Value of type ‘Any?’ has no member ‘valueForKey’ 对于以下代码行: let userName : NSString? = result.valueForKey("name") as? NSStringlet facebookID : NSString? = result.valueForKey("id") 嗨我已更新到 swift 3并且收到错误:

Value of type ‘Any?’ has no member ‘valueForKey’

对于以下代码行:

let username : Nsstring? = result.valueForKey("name") as? Nsstringlet facebookID : Nsstring? = result.valueForKey("ID") as? Nsstringlet firstname : Nsstring? = result.valueForKey("first_name") as? Nsstringlet lastname : Nsstring? = result.valueForKey("last_name") as? Nsstringlet 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",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
虽然你找到了答案,但使用纯粹的快速方法而不是使用Nsstring更好,最好不要强制打开选项
FBSDKGraphRequest(graPHPath: "me",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"]
总结

以上是内存溢出为你收集整理的swift3 – Swift 3 – “Any”类型的值没有会员’valueForKey’全部内容,希望文章能够帮你解决swift3 – Swift 3 – “Any”类型的值没有会员’valueForKey’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存