我正在尝试使用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***
}
}
答案 0 :(得分:1)
问题在于您已在 struct
内定义了viewDidLoad
。因此范围仅限于该方法。将其移出viewDidLoad
,但仍在ViewController
,您应该可以从SecondViewController
访问它。显然,您也必须修改对myEmail
的引用,因为它被称为email
。此外,在您的SecondViewController
中,您应该将viewDidLoad
实施从verticalSliderChanged
方法中拉出来; viewDidLoad
应该是SecondViewController
的顶级实例方法,未在另一个方法中定义。
但这里存在更深层次的问题。您应该制作这些简单的实例变量,而不是将struct
与static
变量一起使用,创建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
}
}