title: Swift iOS -How to Create A Property with Getter and Setter on Extension of UIViewController Class
body: <p>I know in Swift your not allowed to use properties inside extensions.</p>
  
  <p>I have a property and several functions that I use throughout my app in several different classes. I want to extend the UIViewController class to include the property and functions. I know that you can't add properties to extensions in Swift. I tried a computed property but it wouldn't accept that either so I resorted to a getter and setter. It's accepting it but it's not working properly because the property isn't getting set.</p>
  
  <p><strong>What I'm looking to do:</strong></p>
  
  <p>I want to be able to call steps 4 and 5 in different classes instead of using this same code over and over. Steps 4 and 5 access the same property which is initialized from another function inside the class.</p>
  
  <p><strong>Steps:</strong></p>
  
  <p>I add the property with a getter/setter and 3 functions as extensions to the UIViewController class.</p>
  
  <ol>
  <li><p>the property type is a UIView that gets initialized from a function inside the class.</p></li>
  <li><p>the function contains a label, a localView (type uiview), and an activity indicator. This function adds the label and actInd as subViews to the localView and then returns the localView.</p></li>
  <li><p>the property from step one gets initialized with return value from the function in step 2.</p></li>
  <li><p>another function accesses the property and adds it as a subView to the UIViewController class</p></li>
  <li><p>another function accesses the same property and removes it from the UIViewController class.</p></li>
  </ol>
  
  <p>I tried to use a getter and setter but it's not working.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>extension UIViewController{
  
     //step 1
     var stepOneSetView: UIView? {
             get  {
                  return UIView()
             }set {
                  return  self.stepOneSetView = stepTwoCreateViewWithLabelAndActInd()
             }
     }
  
     //step 2
     func stepTwoCreateViewWithLabelAndActInd() -&gt; UIView{
  
          var localView = UIView()
          var actInd = UIActivityIndicatorView()
          var label = UILabel()
  
          //do whatever then add actInd and label to localView
          return localView
     }
  
     //step 3 is inside the setter of stepOneSetView
  
     //step 4
     func stepFourAddViewFromStepOneToViewController(){
  
          view.addSubview(stepOneCreateView!)
     }
  
     //step 5
     func stepFiveRemoveViewFromStepOneFromViewController(){
  
         stepOneCreateView!.removeFromSuperview()
     }
  }
  </code></pre>
  
  <p>Now when I subclass the UIViewController class I can use functions step 4 and 5:</p>
  
  <pre><code>class BlaBlaBla: UIViewController{
  
     override func viewDidLoad() {
             super.viewDidLoad()
  
             self.stepFourAddViewFromStepOneToViewController()
  
             //some timer happens and then
  
             self.stepFiveRemoveViewFromStepOneFromViewController()
     }
  }
  </code></pre>
  
  <p>Where am I going wrong at?</p>