1

我正在尝试使用 NSTimer 函数每五秒检查一次请求。除了我收到错误:

MyApp[13952:2483629] *** NSForwarding: warning: object 0x7f98cd15ab80 of class 'MyApp.Requests' does not implement methodSignatureForSelector: -- trouble ahead

Unrecognized selector -[MyApp.Requests checkReq:]

我的代码简化如下:

var timer: NSTimer?

    func parseUberRequest(dict: NSDictionary) {
        if let reqId = dict["request_id"] as? String {
            timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "checkReq:", userInfo: ["reqId" : reqId], repeats: false)
        }
    }

    func checkReq(timer: NSTimer) {
        let userInfo = timer.userInfo as! Dictionary<String, String>
        if let reqId = userInfo["reqId"] {
            println(reqId)
        }

    }

请知道,我已经查看了此站点上相同错误的其他答案,并发现它们都是非常过时的答案。这是 XCode 6.4 和 Swift 1.2。此处不涉及 Objective-C。

我也尝试使用 Selector("checkReq:") 无济于事。

4

2 回答 2

1

此代码所属的类(它必须是类,而不是结构或枚举)必须继承自NSObject.

更严格地说,无论您放入target:参数中的哪个类都NSTimer.scheduledTimerWithTimeInterval必须继承自NSObject. 在这种情况下,它恰好是self在另一种情况下,它可能不是。

于 2015-08-29T17:22:44.637 回答
1

Cocoa 的目标选择器模式需要符合 NSObject 协议。

让你的类成为 NSObject 的子类

于 2015-08-29T17:23:21.670 回答