Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift fbsdk图形请求后,变量值变为零_Swift_Variables_Fbsdk - Fatal编程技术网

Swift fbsdk图形请求后,变量值变为零

Swift fbsdk图形请求后,变量值变为零,swift,variables,fbsdk,Swift,Variables,Fbsdk,我正在用swift写一个项目,我对这门语言相当陌生 我将变量“username”的值设置为我向facebook sdk请求的结果,但在图形请求之后,我的变量值变为零。变量赋值在图形请求本身内部运行良好 这是我的密码 import UIKit import FBSDKCoreKit import FBSDKLoginKit import FBSDKShareKit class SecondViewController: UIViewController { var username :

我正在用swift写一个项目,我对这门语言相当陌生

我将变量“username”的值设置为我向facebook sdk请求的结果,但在图形请求之后,我的变量值变为零。变量赋值在图形请求本身内部运行良好

这是我的密码

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit

class SecondViewController: UIViewController {

    var username : String = "X"
    let label = UILabel()


    override func viewDidLoad() {
        super.viewDidLoad()

        FBSDKGraphRequest(graphPath: "/me", parameters: ["Fields" : "name"]).start {

            (connection, result, err) in

            if err != nil {
                print("Failed to start request", err)
                return
            }

            var res = result as? [String : Any]
            self.username = (res?["name"] as! String)

            print(self.username)


        }


        print(self.username)
    }

}
请求内部的第一个print语句工作正常,但请求外部的第二个print语句打印空行


当我想将标签的文本设置为username变量的值时,也是这样。它在请求内部工作,但没有做任何其他事情

我认为首先要执行登录权限:

    loginManager.logIn(withReadPermissions: ["email","public_profile"], from: self) { (FBSDKLoginManagerLoginResult, Error) in

        if(Error == nil){
            if(FBSDKLoginManagerLoginResult?.isCancelled)!{
                SVProgressHUD.showError(withStatus: "You Cancelled To Login With Facebook, \n Try Again Later!")
            }else if(FBSDKLoginManagerLoginResult?.token != nil){
                SVProgressHUD.showSuccess(withStatus: "Login Successfull")

                SVProgressHUD.show(withStatus: loadingText)

                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 resultDic = result as! [String: Any]
                            let facebookProfileUrl : String! = "http://graph.facebook.com/\((FBSDKLoginManagerLoginResult?.token!.userID)!)/picture?type=large"
                            var fbId = (FBSDKLoginManagerLoginResult?.token!.userID)! as String
                            var name = resultDic["name"] as! String      
                            var email = resultDic["email"] as! String

                        }else{
                            print(error!.localizedDescription)
                        }
                    })
                }
            }
        }
    }
由于您的内部代码工作正常,这意味着您在上面的表现很好,我认为它在线程中工作,这意味着您的底部“print(self.username)”首先被打印,然后在块内部的上面被打印


您可以在完成Graph API响应后记录它

我认为首先要执行登录权限:

    loginManager.logIn(withReadPermissions: ["email","public_profile"], from: self) { (FBSDKLoginManagerLoginResult, Error) in

        if(Error == nil){
            if(FBSDKLoginManagerLoginResult?.isCancelled)!{
                SVProgressHUD.showError(withStatus: "You Cancelled To Login With Facebook, \n Try Again Later!")
            }else if(FBSDKLoginManagerLoginResult?.token != nil){
                SVProgressHUD.showSuccess(withStatus: "Login Successfull")

                SVProgressHUD.show(withStatus: loadingText)

                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 resultDic = result as! [String: Any]
                            let facebookProfileUrl : String! = "http://graph.facebook.com/\((FBSDKLoginManagerLoginResult?.token!.userID)!)/picture?type=large"
                            var fbId = (FBSDKLoginManagerLoginResult?.token!.userID)! as String
                            var name = resultDic["name"] as! String      
                            var email = resultDic["email"] as! String

                        }else{
                            print(error!.localizedDescription)
                        }
                    })
                }
            }
        }
    }
由于您的内部代码工作正常,这意味着您在上面的表现很好,我认为它在线程中工作,这意味着您的底部“print(self.username)”首先被打印,然后在块内部的上面被打印


您可以在完成Graph API响应后记录它

这是因为它是异步的。如果您在两个打印中添加不同的字符,您会发现第二个将在第一个之前打印。这是因为它是异步的。如果你在两张照片中添加不同的字符,你会发现第二张照片将在第一张之前打印。