从函数返回数据 (Swift)

Returning Data From Function (Swift)

我正在尝试 return 结果并能够从此函数访问结果数组。函数中的一切都在工作,但是我无法 return 任何东西或访问结果或从闭包外部在函数内部创建的任何变量。我想从闭包外部访问 result.valueForKey("id") 。我该怎么做?

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


        }
    })
}

编辑:我确实根据下面的 2 个答案更改了代码,但我仍然无法在 viewDidLoad 闭包上获取任何数据 return。 * println(facebookid) 不会 return ViewDidLoad 中的任何内容,而它会在函数的闭包内部执行。

您正在闭包内创建变量,这意味着它们只能在内部访问。

您需要在函数外部创建变量 class。

例如

 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

    }
  })
 }
}
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)!)")
                        }
                    })
                }

    }