Xcode NSTimer error: 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() 时,我收到一条错误消息 "unrecognized selector sent to class"。我不知道我在这里做错了什么。
class func sayHello() {
print("hello")
}
请将此设为 class 方法。
我已经在此处更新了您的代码:
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)
}
我有一个名为 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() 时,我收到一条错误消息 "unrecognized selector sent to class"。我不知道我在这里做错了什么。
class func sayHello() {
print("hello")
}
请将此设为 class 方法。
我已经在此处更新了您的代码:
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)
}