I need my app to pass the value of a variable myVariable
from a class firstClass
to another secondClass
only when the variable changed its value. To do so, I thought of using the willSet
property. Though, in Swift, you can't use it after the declaration of the variable.
class firstClass: NSObject {
var myVariable = 0
func myFunction {
myVariable = 5
}
}
class secondClass: NSObject {
var otherClass = firstClass()
// How do I retrive the value of the variable right after its value changed?
}
I also thought of adding a NSNotification
, but that wouldn't help because it doesn't pass a value. NSNotification
only alerts about its changes.
let myVariableNotification = NSNotification(name: "myVariableNotification", object: nil)
class firstClass: NSObject {
var myVariable = 0
func myFunction {
myVariable = 5
notificationCenter.postNotification(myVariableNotification)
}
}
class secondClass: NSObject {
var otherClass = firstClass()
NSNotificationCenter.defaultCenter().addObserverForName("myVariableNotification",
object: nil,
queue: NSOperationQueue.mainQueue()
usingBlock: { notification in
println("The variable has been updated!")
})
}
I seem to find no way to pass a variable once that variable changed its value. How can I do that?