Animation WatchKit";已将无法识别的选择器发送到实例";尝试在Swift中激发NSTimer对象时
在我的WatchKit extensions InterfaceController.swift文件中,我从awakeWithContext函数中调用了一个函数,该函数运行动画图像序列 我使用了一个NSTimer对象来计时延迟,然后触发第二个动画序列Animation WatchKit";已将无法识别的选择器发送到实例";尝试在Swift中激发NSTimer对象时,animation,watchkit,unrecognized-selector,Animation,Watchkit,Unrecognized Selector,在我的WatchKit extensions InterfaceController.swift文件中,我从awakeWithContext函数中调用了一个函数,该函数运行动画图像序列 我使用了一个NSTimer对象来计时延迟,然后触发第二个动画序列 var-nsTimerObject:NSTimer=NSTimer.scheduledTimerWithTimeInterval(showCodeDelay,目标:self,选择器:“showCode(counterCode1)”,userInfo
var-nsTimerObject:NSTimer=NSTimer.scheduledTimerWithTimeInterval(showCodeDelay,目标:self,选择器:“showCode(counterCode1)”,userInfo:nil,repeats:false)代码>
在第一个动画运行后,我收到一个错误,该错误阻止在选择器中指定的函数触发第二个动画:
[Code\u Break\u WatchKit\u Extension.InterfaceController showCode(计数器代码1)]:发送到实例0x6080014c3f0的未识别选择器
我在目标成员资格中勾选了iOS设备应用程序和WatchKit扩展
我在堆栈上找到了溢出,它使我沿着这条路线前进
另一个似乎建议我可能需要做一些关于“分享”的事情,但不清楚
感觉应该很简单,但我被难住了
我是新手,所以请对我温柔一点:)把它整理好
问题在于设置选择器函数和传递函数工作所需的参数的语法
我改变了:
var nsTimerObject:NSTimer = NSTimer.scheduledTimerWithTimeInterval(showCodeDelay, target: self, selector: "showCode(counterCode1)", userInfo: nil, repeats: false);
致:
然后,我不得不更改showCodeDelay函数声明,从:
func showCodeDelay(counterCode: Int){
//code........
}
致:
我只是想弄清楚,我第一次错误地认为我只是简单地将选择器指向我用所需参数创建的函数
但是,采用这种方法时,选择器中引用的函数必须接收实际的NSTimer对象,并从中提取用户信息所需的参数
一旦我解决了这个问题,我的下一个问题就是提取我在userInfo中传递的值,因为Xcode希望我将其视为任何对象不能表示为Int,因此我从nsTimerObject对象中提取userInfo作为NSNumber,然后将其向下转换为Int
也许有一种更简单、更优雅的方式来达到沮丧的效果,但至少这是可行的
func showCodeDelay(counterCode: Int){
//code........
}
func showCodeDelay(nsTimerObject:NSTimer){
var tempCounterCode = nsTimerObject.userInfo as! NSNumber
var counterCode = Int(tempCounterCode)
//code........
}