gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:51:46 24 4
gpt4 key购买 nike

您好,我已更新到 swift 3,但收到错误:

Value of type 'Any?' has no member '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

最佳答案

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

关于swift3 - Swift 3 - 类型 'Any?' 的值没有成员 'valueForKey',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com