gpt4 book ai didi

ios - Swift2- NSTimer崩溃

转载 作者:行者123 更新时间:2023-12-03 17:48:35 29 4
gpt4 key购买 nike

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

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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com