Accessing variables from another ViewController in Swift

Everything by default in swift is public, and thus if you declare something like this:

class SomeViewController: UIViewController {
    var someVariable: SomeType = someValue

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
}

You can access it as long as you have an instance of it:

var myCustomViewController: SomeViewController = SomeViewController(nibName: nil, bundle: nil)
var getThatValue = myCustomViewController.someVariable

Leave a Comment