Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Value of type 'Any?' has no member 'valueForKey'

Tags:

swift3

Hi I have updated to swift 3 and am getting the error:

Value of type 'Any?' has no member 'valueForKey'

for the following lines of code:

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

What is the explanation for theses errors and how would I go about fixing it?

Many thanks!

EDIT:

Full Code:

  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
                    

ANSWER:

  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
like image 765
MattBlack Avatar asked Sep 14 '16 12:09

MattBlack


1 Answers

Though you find an answer but its better to use pure swift approach rather than using NSString and better not to force unwrap optionals

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"] 
like image 176
EI Captain v2.0 Avatar answered Nov 03 '22 02:11

EI Captain v2.0