使用Struct传递变量Swift
我正在尝试使用一个Struct来传递一些从Facebook图形请求中获得的变量,比如电子邮件、姓名、性别等。 我已在“ViewController”中创建了结构(“fbDemographics”)和变量,但在尝试调用“SecondViewController”中的结构和其中一个变量时出错(“ViewController”类型没有成员“fbDemographics”)。我以前从未使用过struct,所以有点困惑为什么会出现这个错误。谢谢你的想法。两个视图控制器的代码如下所示:使用Struct传递变量Swift,swift,variables,struct,Swift,Variables,Struct,我正在尝试使用一个Struct来传递一些从Facebook图形请求中获得的变量,比如电子邮件、姓名、性别等。 我已在“ViewController”中创建了结构(“fbDemographics”)和变量,但在尝试调用“SecondViewController”中的结构和其中一个变量时出错(“ViewController”类型没有成员“fbDemographics”)。我以前从未使用过struct,所以有点困惑为什么会出现这个错误。谢谢你的想法。两个视图控制器的代码如下所示: class View
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***
}
}
问题是您已经在
viewDidLoad
中定义了struct
。因此,范围仅限于该方法。将其移出viewdiload
,但仍在ViewController
中,您应该能够从SecondViewController
访问它。显然,您也必须修复对myEmail
的引用,因为它被称为email
。另外,在您的SecondViewController
中,您应该将viewDidLoad
实现从verticalSliderChanged
方法中拉出;viewDidLoad
应该是SecondViewController
的顶级实例方法,而不是在另一个方法中定义
不过,这里还有更深层次的问题。与其将struct
与static
变量一起使用,您真的应该创建那些简单的实例变量,为您的FbDemographics
类型创建一个实例(注意,用大写字母开始struct
类型),然后传入此实例
例如,传递数据的正确方法是:
- 消除
静态
变量李>
- 给你的
一个以大写字母开头的名字李>struct
- 创建
;及结构的实例
- 将此实例传递到中的目标视图控制器
- 给你的
class SecondViewController: UIViewController {
var demographics: FbDemographics!
override func viewDidLoad() {
super.viewDidLoad()
let value = demographics.email // this should work fine here
}
}
您当前的解决方案存在很多问题。我会搜索一些关于在视图控制器之间传递数据的教程。我已经阅读了很多教程。只是没有传递变量,但我想我会继续努力。谢谢你看。你的问题有很多不必要的代码。请删除并保留重要的内容。非常感谢您花时间给出如此完整的答案。我经历了这一切,并按照你说的去做。我没有任何错误,这是一个良好的开端。但有一个问题;我不知道你说的通过实例(prepareforsgue)是什么意思。再次感谢。出于种种原因,声明
static
属性是不明智的(其中FbDemographics
的每个实例都将共享相同的值)。相反,您希望使用let foo=FbDemographics(…)
语法创建一个“实例”。然后,问题变成了SecondViewController
如何获取此实例的副本。因此,您需要准备(for:sender:)
确认目标是SecondViewController
,如果是,则将相应的demographics
属性指定为我们上面创建的原始FbDemographics
实例的副本。
class SecondViewController: UIViewController {
var demographics: FbDemographics!
override func viewDidLoad() {
super.viewDidLoad()
let value = demographics.email // this should work fine here
}
}