-2

I'm new in iOS.

I'm trying to pass my array data to another view controller through prepareforsegue method.

And this api is called inside the @IBAction func ButtonTapped( ).

class FirstVc {
var location = [Any]()
  self.clientrequest.request(url: "http://api.details.in/api/users/alllocation", method: .GET, completion: {
                    res , err in           
let json = try! JSONSerialization.data(withJSONObject: res , options: .prettyPrinted)
let decode = try! JSONDecoder().decode(SelectLocation.self, from:json)

            self.location = decode.states
   //I'm getting all locations here
               print(self.location)

  })
}
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.destination is SecondVc
        {
            let vc = segue.destination as?  SecondVc
     //here I'm assigning the variable and while I'm printing location here then also I'm getting the value
                vc?.mineSpillere2 = self.location
        }
    }

class SecondVc{
 @IBOutlet weak var selectState: UILabel!
    var mineSpillere2 = [Any]()
override func viewDidLoad() {
//While I'm trying to print mineSpillere2 here I', getting []
     selectState.text = mineSpillere2 as? String
    }    
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
Ronak Patel
  • 609
  • 4
  • 16

3 Answers3

0

One problem might be that you don't actually assign the variable (because vc may be nil). Also you may want to use segueIdentifiers, especially if you have more than one. To make sure that you do so, you may want to use something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? SecondVc //, segue.identifier = "YOUR_SEGUE_ID"
    {
        vc.mineSpillere2 = self.location
    } else { print("SecondVc couldn't be initialised!") }
}
thisIsTheFoxe
  • 1,584
  • 1
  • 9
  • 30
-1

There are several way to pass this in another ViewController. Good way is make a model class and pass model object to another vc . Or might be another short and easy solution is make Global variable. So you can access it from any ViewController class. like

    var myGloblArray= [Any]()
//add your result array in this array , you can access it from second VC Directly.

    class FirstVc {
    ...

    }
Nahid Raihan
  • 957
  • 1
  • 10
  • 20
-1

First, the problem is that web service request is not executed asynchronously but synchronously. In other words, your segue is already performed before decode.states is assigned to the location variable.

The solution is to call performSegue function in the completion block:

self.clientrequest.request(url: "http://api.details.in/api/users/alllocation", method: .GET, completion: {
                res , err in           
let json = try! JSONSerialization.data(withJSONObject: res , options: .prettyPrinted)
let decode = try! JSONDecoder().decode(SelectLocation.self, from:json)

        self.location = decode.states
        self.performSegue(identifier: "your identifier", 
       sender: nil)
//I'm getting all locations here
           print(self.location)

})

Second, Make sure your segue is assigned from VC1 to VC2 not Button to VC2.

Soroush
  • 541
  • 4
  • 17
  • are you sure you are parsing server response correctly? set a breakpoint right before performing segue and check if self.location is assigned properly. – Soroush Sep 25 '19 at 08:06
  • I printed the value of self.location in func prepare(for segue: UIStoryboardSegue, sender: Any?) then also I'm getting my output.... after this I'm assigning this output to the variable of SecondVc.. – Ronak Patel Sep 25 '19 at 08:11
  • so you mean you have your data in `prepare(for segue: UIStoryboardSegue, sender: Any?)` but when the segue is performed, you do not have your data in the next View Controller? – Soroush Sep 25 '19 at 08:19
  • check the storyboard one more time and make sure your segue is correctly assigned. I presume you have set multiple segues by mistake. Also set an identifier for it and check it in an if condition inside `prepareForSegue` function – Soroush Sep 25 '19 at 08:26
  • I have created a segue from button to the SecondVc and I had given identifier also.. And why we are assigning ```self.performSegue(identifier: "your identifier", sender: nil)``` sender as nil – Ronak Patel Sep 25 '19 at 08:38
  • Do not assign it through the button. Assign it from VC1 to VC2 not from button to VC2. – Soroush Sep 25 '19 at 09:02
  • yes sir.. I got my answer .. I assigned the segue from VC1 to VC2... THANKS ALOT.... – Ronak Patel Sep 25 '19 at 09:18