我试图使用一个结构来传递我从Facebook图形请求获得的一些变量,如电子邮件,姓名,性别等 我创建了Struct('fbDemographics ')和'ViewController'中的变量,但当我尝试调用结构和'SecondViewController'中的一个变量(类型'ViewController'没有成员'fbDemographics')时出现错误。我从来没有使用过结构,所以有点困惑,为什么我得到这个错误。感谢您的任何想法。两个视图控制器的代码如下:使用结构来传递变量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)
}
}
第二视图控制器
class SecondViewController: UIViewController {
@IBAction func verticalSliderChanged(_ sender: UISlider) {
let currentValue = String(sender.value);
sliderLabel.text = "\(currentValue)"
func viewDidLoad() {
super.viewDidLoad()
***ViewController.fbDemographics.myEmail***
}
}
有很多的东西错了你目前的解决方案。我会搜索一些关于在视图控制器之间传递数据的教程。 – nathan
我已经经历了很多教程。只是没有获得传递变量,但我想生病继续堵塞。谢谢参观。 –
你的问题有很多不必要的代码。请删除并保留唯一重要的内容。 – Macabeus