Search code examples
swiftclassinstanceexecute

How it can execute classes in swift without instance?


I am new to swift and there is something that i couldn't understand and it is about classes execute. I thought that class can't execute them self , you need to define an instance variable to work with the class's methods and properties but i noticed in xcode files that the classes don't have in instance variable why is that? and how the class get executed itself without an instance ?

Thanks advance

    import UIKit

class ViewController: UIViewController {
    var theView: UIView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.redColor()

    }

}
/// Why i donl't need this line of code to worke with the class above
/// instead the class above execute itself without this instance
var theViewControllerInstance = ViewController()

Solution

  • You can create a ViewController instance and do all the work by your self, but a more commonly way is to bind this class in interface builder (storyboard or xib), when app start and load UI resource, the instance is created for you.