1

I am trying to run the code below so that my ThirdViewController can mirror its "myOrder" variable based off of the one in SecondViewController, but I am getting a runtime error "Swift dynamic cast failed" when the debugger gets to the line that I marked with the note "error here" below. I believe I am using Xcode 6.1.1 and Swift 1.1. How come SecondViewController is not being accepted as a valid type?

import UIKit

class ThirdViewController: UIViewController {

    var order1Text = String()
    var myOrder = OrderModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        let barViewControllers = self.tabBarController?.viewControllers
        let svc = barViewControllers![1] as SecondViewController //error here
        svc.myOrder = self.myOrder  

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override func viewWillAppear(animated: Bool) {
        order1Label.text = myOrder.currentOrder()

    }

    @IBOutlet var order1Label: UILabel!
}
Pang
  • 9,564
  • 146
  • 81
  • 122
shampouya
  • 386
  • 1
  • 6
  • 24
  • Have you verified what type of view controller is returned by the call to `barViewControllers![1]` - if that view controller is embedded in a navigation controller, for instance, what you'd get would be the navigation controller and not the `SecondViewController`. So make sure you have the right type of view controller there. – Fahim Mar 26 '17 at 09:40
  • 3
    You should not access data from your secondViewController. You should **pass the data** to your thirdViewController before pushing it in the first place. –  Mar 26 '17 at 09:42

0 Answers0