title: Execute a method when a variable value changes in Swift
body: <p>I need to execute a function when a variable value changes. </p>
  
  <p>I have a singleton class containing a shared variable called <code>labelChange</code>. Values of this variable are taken from another class called <code>Model</code>. I have two VC classes, one of them has a button and a label and the second only a button.</p>
  
  <p>When the button in the first VC class is pressed I am updating the label with this func:</p>
  
  <pre><code>func updateLabel(){
      self.label.text = SharingManager.sharedInstance.labelChange
  }
  </code></pre>
  
  <p>But I want to call the same method whenever the value of the <code>labelChange</code> is changed. So in button click I will only update the <code>labelChange</code> value and when this thing happen I want to update the label with the new value of the <code>labelChange</code>. Also in the second VC I am able to update the <code>labelChange</code> value but I am not able to update the label when this value is changed.</p>
  
  <p>Maybe properties are the solution but can anyone show me how to do so.</p>
  
  <p><strong>Edited second time:</strong></p>
  
  <p>Singleton Class:</p>
  
  <pre><code>class SharingManager {
      func updateLabel() {
          println(labelChange)
          ViewController().label.text = SharingManager.sharedInstance.labelChange     
      }
      var labelChange: String = Model().callElements() {
          willSet {
              updateLabel()
          }
      }
      static let sharedInstance = SharingManager()
  }
  </code></pre>
  
  <p>First VC:</p>
  
  <pre><code>class ViewController: UIViewController {
      @IBOutlet weak var label: UILabel!
      @IBAction func Button(sender: UIButton) {    
         SViewController().updateMessageAndDismiss()
      }
  }
  </code></pre>
  
  <p>Second VC:</p>
  
  <pre><code>func updateMessageAndDismiss() {
          SharingManager.sharedInstance.labelChange = modelFromS.callElements()
          self.dismissViewControllerAnimated(true, completion: nil)
      }
  @IBAction func b2(sender: UIButton) { 
          updateMessageAndDismiss()
  }
  </code></pre>
  
  <p>I made some improvements but I need to reference a label from the first VC class in singleton. Therefore I will update that label of VC in singleton.</p>
  
  <p>When I print the value of <code>labelChange</code> the value is being updated and everything is fine. But when I try to update that value on label from singleton I receive an error:</p>
  
  <blockquote>
    <p>unexpectedly found nil while unwrapping an Optional value</p>
  </blockquote>
  
  <p>and the error is pointing in 4th line of singleton class.</p>