i got two swift files :
main.swift
and view.swift
In main.swift i have a variable (Int)
initially set to 0
.
With an IBACtion
I set that variable
to be 10
, and everything is ok.
However, if I try access that variable
from view.swift, with a simple call like main().getValue()
, i get always 0
and not 10
even if the variable has changed it's value in main.swift.
The method getValue()
in main.swift looks like this:
func getValue() -> Int {
return variable
}
EDIT
Here is the code (Translated from Italian :D )
import Cocoa
class Main: NSObject {
var variable: Int = 0
func getValue() -> Int {
return variable
}
@IBAction func updateVar(sender: AnyObject!) {
variable = 10
}
}
class View: NSView {
override func drawRect(dirtyRect: NSRect) {
println(Main().getValue()) //Returns always 0
}
}
Thanks in advance Alberto
main()
you are creating a new instance of themain
class with the default value (0) forvariable
main
the name of your class (normally classes are capitalized)? Also, are you using aXib
file? It is strange to me that are usingIBAction
with a class called simplymain
. Normally it would be a subclass of a UIKit or Cocoa view or controller class.