Struct 视图控制器之间的快速传递结构?
我的应用让用户登录到他们的帐户。然后,应用程序访问Parse.com来检索他们的所有信息。该信息存储在结构中。我希望在视图控制器之间传递该结构,以便可以在应用程序中随时访问它。我尝试过的每件事都会给我带来错误。我不能将其声明为可选,也不能在下一个类中设置相同的结构。此问题的解决方案是什么?使用以下代码在ViewController之间传递structStruct 视图控制器之间的快速传递结构?,struct,swift,Struct,Swift,我的应用让用户登录到他们的帐户。然后,应用程序访问Parse.com来检索他们的所有信息。该信息存储在结构中。我希望在视图控制器之间传递该结构,以便可以在应用程序中随时访问它。我尝试过的每件事都会给我带来错误。我不能将其声明为可选,也不能在下一个类中设置相同的结构。此问题的解决方案是什么?使用以下代码在ViewController之间传递struct //FirstViewController.swift struct GlobalStruct { var de
//FirstViewController.swift
struct GlobalStruct
{
var details:String;
init()
{
details = "global struct";
}
};
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonClicked()
{
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("secondview") as SecondViewController
var passData = GlobalStruct();
passData.details = "Secret Information :)";
secondViewController.setStructDataReference(passData); //passStruct
self.navigationController.pushViewController(secondViewController, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//SecondViewController.swift
class SecondViewController:UIViewController
{
var structData:GlobalStruct;
override func viewDidLoad()
{
println("struct data = \(self.structData.details)");
}
func setStructDataReference(structDataReference:GlobalStruct)
{
self.structData = structDataReference;
}
init(coder aDecoder: NSCoder!)
{
self.structData = GlobalStruct();
super.init(coder: aDecoder);
}
}
我将在两个控制器上创建空数组[Struct]。 然后,无论您在
performsgue
之前执行什么操作,您都应该执行:
self.arrayinmaincontroller.insert(结构,位于:0)
并将其传递给func prepare(对于segue:UIStoryboardSegue,sender:Any?是否将其包装到类中?为什么坚持使用结构?应该能够在视图控制器之间传递结构(或类),而不会出现问题。显示您在哪里进行传递的代码,并告诉我们您在代码运行时遇到了哪些错误或未侦听的行为。如何将其包装到类中?var主体和一些视图非常混乱