使用Struct传递变量Swift

时间:2017-05-06 17:31:21

标签: swift variables struct

我正在尝试使用Struct来传递我从Facebook图形请求中获得的一些变量,例如电子邮件,姓名,性别等。 我在'ViewController'中创建了Struct('fbDemographics')和变量,但是当我尝试调用struct并且'SecondViewController'中的一个变量时,我得到一个错误(Type'ViewController'没有成员'fbDemographics' )。我之前从未使用过struct,所以有点困惑为什么我会收到这个错误。谢谢你的任何想法。两个视图控制器的代码如下:

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    struct fbDemographics {

        static var relationship_status: String?
        static var gender: String?
        static var user_education_history: String?
        static var user_location: String?
        static var email: String?
        static var name: String?

    }     

    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, relationship_status, gender, user_location, user_education_history, email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
            //let fbDetails = result as! NSDictionary
            //print(fbDetails)

            if let userDataDict = result as? NSDictionary {
                fbDemographics.gender = userDataDict["gender"] as? String
                fbDemographics.email = userDataDict["email"] as? String
                fbDemographics.name = userDataDict["name"] as? String
                fbDemographics.user_location = userDataDict["user_location"] as? String
                fbDemographics.user_education_history = userDataDict["user_education_history"] as? String
                fbDemographics.relationship_status = userDataDict["relationship_status"] as? String


                let myEducation = fbDemographics.user_education_history
                let myEmail = fbDemographics.email
                let myGender = fbDemographics.gender
                let myName = fbDemographics.name
                let myStatus = fbDemographics.relationship_status
                let myLocation = fbDemographics.user_location                   

                self.performSegue(withIdentifier: "LoginToHome", sender: (Any).self)


            }


        }

第二视图控制器

class SecondViewController: UIViewController {

@IBAction func verticalSliderChanged(_ sender: UISlider) {

        let currentValue = String(sender.value);

        sliderLabel.text = "\(currentValue)"

    func viewDidLoad() {
        super.viewDidLoad()

        ***ViewController.fbDemographics.myEmail***

    }
}

1 个答案:

答案 0 :(得分:1)

问题在于您已在 struct内定义了viewDidLoad 。因此范围仅限于该方法。将其移出viewDidLoad,但仍在ViewController,您应该可以从SecondViewController访问它。显然,您也必须修改对myEmail的引用,因为它被称为email。此外,在您的SecondViewController中,您应该将viewDidLoad实施从verticalSliderChanged方法中拉出来; viewDidLoad应该是SecondViewController的顶级实例方法,未在另一个方法中定义。

但这里存在更深层次的问题。您应该制作这些简单的实例变量,而不是将structstatic变量一起使用,创建FbDemographics类型的实例(注意,使用大写字母开始struct类型) ,然后在prepare(for:sender:)中传递此实例。

例如,传递数据的正确方法是:

  • 消除static个变量;
  • 给你的struct一个以大写字母开头的名字;
  • 创建struct的实例;和
  • 将此实例传递到prepare(for:sender:)中的目标视图控制器。

E.g。

struct FbDemographics {
    var relationshipStatus: String?
    var gender: String?
    var userEducationHistory: String?
    var userLocation: String?
    var email: String?
    var name: String?
}

class ViewController: UIViewController {

    var demographics: FbDemographics?

    override func viewDidLoad() {
        super.viewDidLoad()

        performRequest()   // I actually don't think you should be initiating this in `viewDidLoad` ... perhaps in `viewDidAppear`
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? SecondViewController {
            destination.demographics = demographics
        }
    }

    func performRequest() {
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, relationship_status, gender, user_location, user_education_history, email"]).start { connection, result, error in
            guard let userDataDict = result as? NSDictionary, error == nil else {
                print("\(error)")
                return
            }

            self.demographics = FbDemographics(
                relationshipStatus: userDataDict["relationship_status"] as? String,
                gender: userDataDict["gender"] as? String,
                userEducationHistory: userDataDict["user_education_history"] as? String,
                userLocation: userDataDict["user_location"] as? String,
                email: userDataDict["email"] as? String,
                name: userDataDict["name"] as? String
            )

            self.performSegue(withIdentifier: "LoginToHome", sender: self)
        }
    }

}

然后SecondViewController可以:

class SecondViewController: UIViewController {

    var demographics: FbDemographics!

    override func viewDidLoad() {
        super.viewDidLoad()

        let value = demographics.email  // this should work fine here
    }

}