Last active
December 2, 2017 10:19
-
-
Save jazzedge/46e4ca069fd9efdfad6e2e009eec65c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See:https://stackoverflow.com/questions/43823430/using-struct-to-pass-a-variable-swift | |
The right way to pass data would be to: | |
eliminate static variables; | |
give your struct a name that starts with uppercase letter; | |
create instance of your struct; and | |
pass this instance to the destination view controller in prepare(for:sender:). | |
01. In the first VC: | |
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) | |
} | |
} | |
} | |
02. In the second (receiving) VC: | |
class SecondViewController: UIViewController { | |
var demographics: FbDemographics! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let value = demographics.email // this should work fine here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment