Persistent data and preparation for Segue

I am trying to use Permanent Data and Prepare For Segue together. This is what I still have: (View Controller 1)

override func viewDidAppear(_ animated: Bool) { let itemsObject = UserDefaults.standard.object(forKey: "items") if let tempItems = itemsObject as? [String] { items = tempItems } 

in (View Controller 2):

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "toSecondViewController" { let itemsObject = UserDefaults.standard.object(forKey: "items") var items:[String] if let tempItems = itemsObject as? [String] { items = tempItems items.append(textField.text!) print(items) } else { items = [textField.text!] } UserDefaults.standard.set(items, forKey: "items") } } 

I am trying to add an element to an array on VC2. Then I want to pass the array to VC1 while saving it. Then every time I close and restart the application, I can print the array. The error message says "Use of unresolved identifier elements." I am using Xcode 8.0 and Swift 3.0.

enter image description here

enter image description here

+6
source share
2 answers

Good, therefore, since you said that you want to save data over the launch of applications, you will need custom default settings for storing your goods. Since Dan already suggested that you basically do it right. You just want to set a variable that was not previously declared. But I will show you this in the following code. I will also take a second approach in which the elements are passed to the next view controller at runtime.

First example: imagine that we have two view controllers, for example, in your example. The first view controller contains a UITextField for entering custom text. Whenever we switch from the first view controller to the second view controller using the storyboard layout (for example, when a button is pressed), we take the existing texts from the previous sections from the user settings by default and add the current user input and then save it back to user defaults. This happens in the first view controller:

 class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) if segue.identifier == "toSecondViewController" { let itemsFromDefaults = UserDefaults.standard.object(forKey: "items") var items: [String] if let tempItems = itemsFromDefaults as? [String] { items = tempItems items.append(textField.text!) } else { items = [textField.text!] } print(items) UserDefaults.standard.set(items, forKey: "items") } } } 

This first controller looks very similar to your code, but I would like to add it for completeness.

Then, in the second view controller, we simply grab the elements from user defaults and store them directly in the instance variable of this view controller. With this, we can do what we want in other methods in the view controller and process the elements further. Since I said that you were missing, there was an instance variable declaration for storing items.

 class ViewController2: UIViewController { private var items: [String]? // This is only accessible privately override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) self.items = UserDefaults.standard.object(forKey: "items") as? [String] } } 

Second example: you can also declare an internal / public variable in ViewController2 so that you can set it directly from the first view controller to execute segue. Than you will not need to capture elements from the user defaults in ViewController2. To do this, you can access the segue destination view controller, and then transfer it to ViewController2 and directly set its elements.

 class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) if segue.identifier == "toSecondViewController" { let itemsFromDefaults = UserDefaults.standard.object(forKey: "items") var items: [String] // [...] Do the stuff to get the items and add current input like before [...] let destinationViewController = segue.destination as! ViewController2 destinationViewController.items = items } } } class ViewController2: UIViewController { var items: [String]? // This is accessible from outside now override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) print(items) // We can now print this because it is set in prepareForSegue } } 

I really hope that I can help you with this and explained it clearly. Feel free to leave a comment if you have any questions.

+1
source

You forgot to write "let" or "var" in viewDidAppear. Try the following:

 override func viewDidAppear(_ animated: Bool) { let itemsObject = UserDefaults.standard.object(forKey: "items") if let tempItems = itemsObject as? [String] { let items = tempItems } } 

If you want to use elements after an if statement, you must declare a variable before if:

 override func viewDidAppear(_ animated: Bool) { let itemsObject = UserDefaults.standard.object(forKey: "items") var items: [String] if let tempItems = itemsObject as? [String] { items = tempItems } } 
+1
source

Source: https://habr.com/ru/post/1011615/


All Articles