ios - Xcode NSTimer 错误 : Unrecognized selector sent to class

标签 ios xcode swift nstimer

我有一个名为 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")
}

请将此设为类方法。

关于ios - Xcode NSTimer 错误 : Unrecognized selector sent to class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417855/

相关文章:

ios - 关于Swift中设置闹钟天数的问题

swift - 运行后无法安装FBSDKCoreKit旧版本 `pod repo update`

objective-c - 将.app集成到另一个应用程序的UIViewcontroller中

ios - 键盘始终显示在 ios 中

ios - 如何从 MKmapview 的可见区域获取半径?

Swiftlint 覆盖与 SPM 相关的项目设置

iphone - 使用 nib 定义 UICollectionViewCell

iphone - 标签文字困惑

ios - 如何将 PFObject 转换为字符串? - Swift 1.2 Xcode 6.4 和 Parse.com -

javascript - 从网站访问动态数据