title: Extension for view controller using Bool in Swift
body: <p>I'm try to make an extension for a view controller. It is a simple view with tap recognizer. If it is pushed once, the background color should change from magenta to  orange. By the next push it should be magenta again.</p>
  
  <p>If I do it for the concrete VC, let say, StartVC, it works. Here is the code:</p>
  
  <pre><code>import UIKit
  
  class StartVC: UIViewController {
      // bool
      var colorChanged = false
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.backgroundColor = UIColor.magenta
          setMyTouchableView2()
      }
  }
  
  extension StartVC {
      // make touchable view
      func setMyTouchableView2() {
          let myView = UIImageView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
          myView.backgroundColor = UIColor.yellow
  
          // add gesture recognizer
          let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeColor2(_:)))
          myView.addGestureRecognizer(tapGestureRecognizer)
          myView.isUserInteractionEnabled = true
  
          view.addSubview(myView)
      }
  
      func changeColor2(_ recognizer:UITapGestureRecognizer){
          if(colorChanged) {
              view.backgroundColor = UIColor.magenta
          } else {
              view.backgroundColor = UIColor.orange
          }
  
          colorChanged = !colorChanged
      }
  }
  </code></pre>
  
  <p>But if I try to generalize this extension for any view controller, I get the following problem: I cant put my boolean (colorChanged) inside the view controller because the extension will not see it. How can I solve it?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
      // bool
      var colorChanged = false
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.backgroundColor = UIColor.magenta
          setMyTouchableView()
      }
  }
  
  extension UIViewController {
      // make touchable view
      func setMyTouchableView() {
          let myView = UIImageView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
          myView.backgroundColor = UIColor.yellow
  
          // add gesture recognizer
          let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeColor(_:)))
          myView.addGestureRecognizer(tapGestureRecognizer)
          myView.isUserInteractionEnabled = true
  
          view.addSubview(myView)
      }
  
      func changeColor(_ recognizer:UITapGestureRecognizer){
          if(colorChanged) {
              view.backgroundColor = UIColor.magenta
          } else {
              view.backgroundColor = UIColor.orange
          }
      }
  </code></pre>