0

i want to send the json array from one view controller to another view controller and the array should be populated in pickerview . but i am unable to send the array . i am getting the array but not able to send it

let mydata = json["data"] as! NSArray    
                                print("My Data is \(mydata)")
                                var sendData = [NSArray]()
                                sendData = mydata as! Array<NSArray>
                                self.performSegue(withIdentifier: "checkLoginViewController", sender: sendData)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let destinationVC = segue.destination as? SignupViewController, let sendData = sender as? [String]{    
                destinationVC.dept = sendData    
            }    
        }

secondVC:

var dept = [String]()
11
  • what error it displays ? Commented Mar 16, 2018 at 10:34
  • I have two questions : What is the purpose of adding let sendData = sender as? [String] in the condition AND is destinationVC.dept = sendData really called ? Commented Mar 16, 2018 at 10:35
  • the error is very big ... being new to swift i am learning as well how to properly debug and understand the error @NikunjDamani Commented Mar 16, 2018 at 10:38
  • i am using that because in stack-overflow i have found this solution soo i have tried that but its not working . so wanted to know if i am using the correct code or not and how i can achieve that @Nitish Commented Mar 16, 2018 at 10:39
  • Tip: you should avoid using NSArray altogether. Stick to Swift arrays. Commented Mar 16, 2018 at 10:39

2 Answers 2

2

The problem is that you use Array<NSArray>

  endData = mydata as! Array<NSArray>

and in prepare cast it like

   let sendData = sender as? [String]

which for sure cast will fail

Sign up to request clarification or add additional context in comments.

8 Comments

@AbhijitPatil : To add on this, you need to check the json response. That should be your starting point so as to which kind of data array you need to take.
can you use let mydata = json["data"] print("\(mydata)") , and attach it so we can guide you to the correct cast
My Data is ( IT , Accounts , HR ) @Sh_Khan
There you go. Take [String] in both cases.
IF IT,Accounts,HR are strings you can use let mydata = json["data"] as! [String]
|
0
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationVC = segue.destination as? SignupViewController, let sendData = sender as? {    
            destinationVC.dept = sendData    
        }    
    }

try this code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.