Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Data From Function (Swift)

I am trying to return result and be able to access result array from this function. Everything is working in the function, however I cannot return anything or access results or any variable created inside the function from outside the closure. I want to access result.valueForKey("id") from outside the closure. How can I do that?

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

var facebookid: NSString = ""
var username: NSString = ""
var userEmail:NSString = ""

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if (FBSDKAccessToken.currentAccessToken() == nil)
    {
        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile"]
        loginView.delegate = self

    } else {

        returnUserData()

        println(facebookid)  // This doesn't work

    }
}



    func returnUserData()
{

    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {

            self.facebookid = result.valueForKey("id") as NSString!
            self.username = result.valueForKey("name") as NSString!
            self.userEmail = result.valueForKey("email") as NSString!
            println(result) // This works


        }
    })
}

Edit: I did change the code according to the 2 answers below but still I still can't get any data return on the viewDidLoad closure. * println(facebookid) doesn't return anything in the ViewDidLoad whereas it does inside the closure in function.

like image 538
senty Avatar asked Aug 25 '26 08:08

senty


2 Answers

You are creating the variables inside the closure meaning they can only be accessed inside.

You need to create the variables outside of the function at the top of you class.

for example

 class YourClassName {

      var facebookid
      var username
      var userEmail


 func returnUserData()
 {
     let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:  "me", parameters: nil)
     graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

    if ((error) != nil)
    {
        // Process error
        println("Error: \(error)")
    }
    else
    {
        self.facebookid = result.valueForKey("id") as? String
        self.userName = result.valueForKey("name") as? String
        self.userEmail = result.valueForKey("email") as? String

    }
  })
 }
}
like image 125
Ryann786 Avatar answered Aug 27 '26 00:08

Ryann786


func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {       
        if error != nil {
            print(error.localizedDescription)
            return
        }


                print("user loged in with facebook")
                if((FBSDKAccessToken.current()) != nil) {
                    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, age_range, link, gender, locale, timezone, updated_time, verified"]).start(completionHandler: { (connection, result, error) -> Void in
                        if (error == nil){
                            print(result!)
                            print("Token: \((FBSDKAccessToken.current().tokenString)!)")
                            print("Token Expiration Date: \((FBSDKAccessToken.current().expirationDate)!)")
                        }
                    })
                }

    }
like image 23
Shiheb Ben Salah Avatar answered Aug 26 '26 23:08

Shiheb Ben Salah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!