如何使用segue将swson数组发送到swift中的不同视图控制器

时间:2018-03-16 10:29:06

标签: ios json swift

我想将json数组从一个视图控制器发送到另一个视图控制器,并且该数组应该在pickerview中填充。但我无法发送数组。我正在获得阵列但无法发送它

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]()

2 个答案:

答案 0 :(得分:2)

问题是你使用Array<NSArray>

  endData = mydata as! Array<NSArray>

并准备演员喜欢

   let sendData = sender as? [String]

肯定会失败

答案 1 :(得分:0)

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

试试这段代码