ios - Swift2- NSTimer崩溃

标签 ios crash double swift2 nstimer

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

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)

提前致谢

最佳答案

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

关于ios - Swift2- NSTimer崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304358/

相关文章:

java-8 - DoubleSummaryStatistics 中的 sum 与 simpleSum 与 sumCompensation?拥有它们有什么重要意义?

ios - 不允许少于 3 个字符的单词

ios - 使用属性动态初始化任何 Objective-C 类

swift - 快速崩溃日志

Android 原生崩溃

c# - 在 C# 中,为什么我会收到 "can' t cast double to float 错误”?

ios - Swift 中的函数重载

ios - 如何知道外部键盘是否在 iOS 中处于事件状态

c# - 如何分析Windows Crash Reporter生成的WERInternalMetadata.xml文件?

Java 最大打印 double 和整数