Don’t think of NSObject
as an Objective-C class, think of it as a Cocoa/Foundation class. Even though you’re using Swift instead of Objective-C, you’re still using all the same frameworks.
Two options: (1) add the dynamic
attribute to the function you want to reference as a selector:
dynamic func timerTick() {
self.angerLevel++
print("Angry! \(self.angerLevel)")
}
Or (2) declare Person
as a subclass of NSObject
, then just call super.init()
at the beginning of your initializer:
class Person: NSObject {
var timer = NSTimer()
var angerLevel = 0
func startTimer() {
print("starting timer")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerTick", userInfo: nil, repeats: true)
}
func timerTick() {
self.angerLevel++
print("Angry! \(self.angerLevel)")
}
override init() {
super.init()
self.startTimer()
}
}