ios - 在 Swift 中使用 NSTimer 查找选择器时出错

标签 ios swift nstimer xcode6.4

<分区>

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

MyApp[13952:2483629] *** NSForwarding:警告:类“MyApp.Requests”的对象 0x7f98cd15ab80 未实现 methodSignatureForSelector:-- 前方有问题

无法识别的选择器 -[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:") 无济于事。

最佳答案

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

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

关于ios - 在 Swift 中使用 NSTimer 查找选择器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288476/

相关文章:

iphone - 在测试用户上安装应用

swift - 惰性属性的内存管理

swift - 为什么我的值只在 swift 第一次打开和关闭应用程序后设置?

ios - 快速调试: app stops at breakpoints and doesn't crash

ios - ios中使用NSTimer启动NSThread

ios - 在 UITabBarController 的顶栏上有一个 UIBarButtonItems

ios - 从 "Over Current Content"ViewController 返回时如何触发一些代码?

ios - NSTimer 上的重置按钮

ios - 同一个 UIView 中的多个 UIPickerView

iOS 在 UILabel 上将时间显示为分数和高分