-4

I am trying to pass the information from one controller to another controller. I have two controllers 1.ViewController 2.SecondController. I want to pass information from ViewController to SecondController using UserDefaults.standard.set where i am trying to get the reference of viewController but i am encountering error saying "assigning non-property list object for key value "controller". Below is the code. Please help me

      let defaults:UserDefaults = UserDefaults.standard
        defaults.set(self, forKey: "controller")
        defaults.synchronize()
sindhu kopparati
  • 223
  • 1
  • 3
  • 7
  • 3
    `UserDefaults` should not be used to pass data. Just set an appropriate property that you have defined on your view controller. – rmaddy Dec 19 '17 at 06:09
  • 2
    And you do not need to call `synchronize`. – rmaddy Dec 19 '17 at 06:10
  • 3
    @DharmeshKheni Even better: https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – rmaddy Dec 19 '17 at 06:22
  • 1
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers). Previous comments are correct, you should not be using UserDefaults to pass data between controllers. The 'Apple way' is to use a segue. See the provided link for the solution. – Scriptable Dec 19 '17 at 08:06

1 Answers1

0

In SecondController declare variable as

  var firstVC: ViewController?

then in ViewController assign self to firstVC while pushing to SecondController like:

  secondVC.firstVC = self
Rahul Dasgupta
  • 894
  • 6
  • 18