从函数返回数据(快速) [英] Returning Data From Function (Swift)

查看:83
本文介绍了从函数返回数据(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试返回结果并能够从此函数访问结果数组.一切都在该函数中运行,但是我无法返回任何内容或从闭包外部访问结果或在函数内部创建的任何变量.我想从闭包外部访问result.valueForKey("id").我该怎么办?

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


        }
    })
}

我确实根据下面的2个答案更改了代码,但仍然无法在viewDidLoad闭包上返回任何数据. * println(facebookid)在ViewDidLoad中不返回任何内容,而在函数的闭包内部返回.

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.

推荐答案

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

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.

例如

 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

    }
  })
 }
}

这篇关于从函数返回数据(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆