字面翻译:向实例发送了不认识的选择器方法。
遇到这个问题:大概有如下两个原因:
- instance对象过早的释放掉了,指针虽然还是指向那块内存地址,但内存实际已经被释放掉了,自然也就无法识别方法了。解决方法:如果是instance是属性的话,首先确认访问修饰符是否正确,比如该用copy的地方错用了retain等;如果不是的话,那就没啥好办法了,加断点,一步步查看源码,看是否多了release,对于每一次instance调用的地方,都打印其内容。
- 该对象没有这个方法,检查一下方法参数跟调用该方法时参数是否匹配。
我遇到的就是第二种情况. 报错代码:
func updateTimer(timer:Timer){
times = times - 1
if times <= 0 {
self.isCounting = false
self.showLabel?.text = "00:00"
self.times = 0
}
}
var isCounting:Bool = false {
willSet(newValue){
if newValue{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: Selector(("updateTimer")), userInfo: nil, repeats: true)
}else{
timer?.invalidate()
timer = nil
}
}
}
运行时一直报错unrecognized selector sent to instance
,看了几遍终于发现是定义updateTimer函数时传入了参数,但是在isCounting调用时没传参数,分析了下,把定义时updateTimer里的参数去掉就不报错了.
func updateTimer( ){
times = times - 1
if times <= 0 {
self.isCounting = false
self.showLabel?.text = "00:00"
self.times = 0
}
}
var isCounting:Bool = false {
willSet(newValue){
if newValue{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: Selector(("updateTimer")), userInfo: nil, repeats: true)
}else{
timer?.invalidate()
timer = nil
}
}
}