[英]+[NSTimer timerWithTimeInterval:block:repeats:]: unrecognized selector sent to class
[英]Xcode NSTimer error: Unrecognized selector sent to class
我有一個名為 Util.swift 的文件,內容如下
class foo: NSObject {
func sayHello() {
print("hello")
}
}
var globalTimer = NSTimer()
func startTheTimer() {
globalTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: foo.self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
}
當我在任何 viewController 的 viewDidLoad 中調用 startTheTimer() 時,我收到一條錯誤消息,提示“無法識別的選擇器已發送到類”。 我不知道我在這里做錯了什么。
class func sayHello() {
print("hello")
}
請將此作為類方法。
我在這里更新了您的代碼:
class foo: NSObject {
class func sayHello() {
print("hello")
}
}
var globalTimer = NSTimer()
func startTheTimer() {
globalTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: foo.self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.