I want to use an NSTimer in a class that doesn't inherit from UIViewVontroller. I have 2 files : a ViewController and a TimerClass like that :
ViewController:
import UIKit
class ViewController: UIViewController {
var timerClass = TimerClass()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
timerClass.launchTimer()
}
}
TimerClass :
import Foundation
class TimerClass {
var timer = NSTimer()
init(){}
func launchTimer(){
var timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "timerEnd", userInfo: nil, repeats: true)
}
func timerEnd(){
println("That worked")
}
}
When I launch that app, I have a crash with :
2015-01-12 19:48:24.322 Timer_TEST[5829:185651] *** NSForwarding: warning: object 0x7fbc3be20710 of class 'Timer_TEST.TimerClass' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Timer_TEST.supportFile timerEnd]
Any idea?
Thank you
EDIT: Note that starting in Swift 2.2 you won't be able to make this mistake! You'll use the new #selector
syntax (see https://stackoverflow.com/a/35658335/341994), and the compiler won't let you form a selector for a method that isn't exposed to Objective-C.
It's merely a question of exposing the Swift function to Objective-C so that it is visible to Objective-C. You have four choices:
Make TimerClass descend from NSObject (and delete the init
implementation):
class TimerClass : NSObject {
Declare TimerClass with @objc
[not in Swift 2.0; use the previous choice instead]:
@objc TimerClass {
Declare the function with @objc
:
@objc func timerEnd()
Declare the function dynamic
(this is probably the worst choice, as it is unnecessary - the function is not dynamic; it does not need to be altered in place by Objective-C, it just needs to be visible):
dynamic func timerEnd(){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With