如何在Swift中从选择器调用私有类函数.

Zai*_*han 10 ios swift

目前我这样做,

将选择器调用为:

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "startAnimation:", userInfo: loadingView, repeats: true)
Run Code Online (Sandbox Code Playgroud)

选择器方法如下:

private class func startAnimation(timer:NSTimer){
    var loadingCircularView = timer.userInfo as UIView
}
Run Code Online (Sandbox Code Playgroud)

我收到警告,应用程序崩溃:

warning: object 0x67c98 of class ‘ClassName’ does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector +[ClassName startAnimation:]
Run Code Online (Sandbox Code Playgroud)

Zai*_*han 1

在类声明时添加 NSObject 解决了我的问题。

参考:NSTimer SchedulerWithTimeInterval 和目标是“类级别函数”

class MyClass:NSObject{}
Run Code Online (Sandbox Code Playgroud)

并将方法调用为,

NSTimer.scheduledTimerWithTimeInterval(0.5, target: ClassName.self, selector: Selector("startAnimation"), userInfo: nil, repeats: true)

class func startAnimation(){}
Run Code Online (Sandbox Code Playgroud)