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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…