Swift2- NSTimer Crashing



当我尝试将计时器设置为动态时,它崩溃了。添加了如下代码,让我知道我在哪里犯了错误:

let defaults = NSUserDefaults.standardUserDefaults()
let tTimer = defaults.stringForKey("ThankYouTimer")
let doubleTimerValue:Double = NSString(string: tTimer!).doubleValue
NSTimer.scheduledTimerWithTimeInterval(doubleTimerValue, target: self, selector: "fireTimer", userInfo: nil, repeats: false)

Thanks in advance

import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
class C {
    func f() {
        print("job")
    }
}
let c = C()
NSTimer.scheduledTimerWithTimeInterval(1, target: c, selector: "f", userInfo: nil, repeats: false)
// 2015-11-06 09:22:55.261 Untitled Page[28568:1397919] *** NSForwarding: warning: object 0x7fd51352d190 of class '__lldb_expr_574.C' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[__lldb_expr_574.C f]

如果你从NSObject中继承C

import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
class C: NSObject {
    func f() {
        print("job")
    }
}
let c = C()
NSTimer.scheduledTimerWithTimeInterval(1, target: c, selector: "f", userInfo: nil, repeats: false)
// print "job" as expected

或者将函数定义为@objc

import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
class C {
    @objc func f() {
        print("job")
    }
}
let c = C()
NSTimer.scheduledTimerWithTimeInterval(1, target: c, selector: "f", userInfo: nil, repeats: false)
// print "job" as expected

最新更新