title: Access variable in different class - Swift
body: <p>i got two swift files :</p>
  
  <p><code>main.swift</code> and <code>view.swift</code></p>
  
  <p><strong>In main.swift</strong> i have a <code>variable (Int)</code> initially set to <code>0</code>.</p>
  
  <p>With an <code>IBACtion</code> I set that <code>variable</code> to be <code>10</code>, and everything is ok.</p>
  
  <p>However, if I try access that <code>variable</code> from <strong>view.swift</strong>, with a simple call like <code>main().getValue()</code>, i get always <code>0</code> and not <code>10</code> even if the variable has changed it's value in main.swift.</p>
  
  <p>The method <code>getValue()</code> in <strong>main.swift</strong> looks like this:</p>
  
  <pre><code>func getValue() -&gt; Int {
  return variable
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the code (Translated from Italian :D )</p>
  
  <pre><code>import Cocoa
  
  class Main: NSObject {
  
      var variable: Int = 0
  
      func getValue() -&gt; Int {
          return variable
      }
  
      @IBAction func updateVar(sender: AnyObject!) {
          variable = 10
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class View: NSView {
      override func drawRect(dirtyRect: NSRect) {
         println(Main().getValue()) //Returns always 0
      }
  }
  </code></pre>
  
  <p>Thanks in advance
  Alberto</p>