2017-05-06 5 views
0

Structを使用して、Facebookのグラフリクエストから取得した変数(電子メール、名前、性別など)を渡そうとしています。 Struct( 'fbDemographics ')と' ViewController 'の変数を使用していますが、' SecondViewController '(タイプ' ViewController 'に' fbDemographics 'というメンバがありません)の変数と構造体を呼び出そうとするとエラーが発生します。私は構造体を使用したことがないので、なぜこのエラーが出るのか分かりません。どんな考えにも感謝します。両方のビューコントローラのコードは以下の通りです:Structを使用して変数を渡すSwift

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) 


      } 


     } 

SECONDビューコントローラ

class SecondViewController: UIViewController { 

@IBAction func verticalSliderChanged(_ sender: UISlider) { 

     let currentValue = String(sender.value); 

     sliderLabel.text = "\(currentValue)" 

    func viewDidLoad() { 
     super.viewDidLoad() 

     ***ViewController.fbDemographics.myEmail*** 

    } 
} 
+0

あなたの現在のソリューションと間違ったことがたくさんあります。ビューコントローラ間でデータを渡す際のチュートリアルを検索します。 – nathan

+0

私は多くのチュートリアルを行ってきました。ただ単に変数を渡すことはありませんが、私は病気が止まっていると思います。見ていただきありがとうございます。 –

+1

質問に不必要なコードがたくさんあります。重要なものだけを削除してください。 – Macabeus

答えて

1

問題は、あなたのstructviewDidLoad内で定義されてきたということです。スコープはそのメソッドに限定されています。 viewDidLoadから移動しますが、まだViewControllerになります。SecondViewControllerからアクセスできるはずです。明らかに、その参照をmyEmailに修正する必要があります。これはemailと呼ばれるためです。また、SecondViewControllerではの実装をverticalSliderChangedメソッドから取り除く必要があります。 viewDidLoadは、別のメソッド内で定義されていないSecondViewControllerの最上位のインスタンスメソッドである必要があります。

ここでも深刻な問題があります。 structstatic変数と一緒に使用するのではなく、単純なインスタンス変数を作成し、FbDemographics型のインスタンスを作成してください(struct型を大文字で開始します)。prepare(for:sender:)にこのインスタンスを渡します。例えば


、データを渡すための正しい方法をすることであろう。

  • static変数を排除します。
  • structに大文字で始まる名前を付けます。
  • structのインスタンスを作成します。
  • このインスタンスを宛先ビューコントローラにprepare(for:sender:)に渡します。

など。その後、

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 
    } 

} 
+0

このような完全な答えを出す時間を取ってくれてありがとう。私はこれを通って、あなたが言ったことに従った。私は良い最初のスタートであるエラーはありません。しかし、1つの質問。あなたがインスタンス(prepareforsegue)を渡すと言ったとき、私はあなたが何を意味しているのか分からなかった。再度、感謝します。 –

+0

無数の理由から、 'static 'プロパティ(' FbDemographics'のすべてのインスタンスが同じ値を共有する)を宣言することはお勧めできません。代わりに 'let foo = FbDemographics(...)'構文で "インスタンス"を作成したいとします。次に、 'SecondViewController'がこのインスタンスのコピーをどのように取得するのかという疑問が生じます。したがって、 'Prepared(for:sender:)'は目的地が 'SecondViewController'であることを確認し、そうであれば対応する' demographics'プロパティを先ほど作成した元の 'FbDemographics'インスタンスのコピーに割り当てます。 – Rob

関連する問題