unrecognized selector sent to instance 报错处理

字面翻译:向实例发送了不认识的选择器方法。


遇到这个问题:大概有如下两个原因:

  1. instance对象过早的释放掉了,指针虽然还是指向那块内存地址,但内存实际已经被释放掉了,自然也就无法识别方法了。解决方法:如果是instance是属性的话,首先确认访问修饰符是否正确,比如该用copy的地方错用了retain等;如果不是的话,那就没啥好办法了,加断点,一步步查看源码,看是否多了release,对于每一次instance调用的地方,都打印其内容。
  2. 该对象没有这个方法,检查一下方法参数跟调用该方法时参数是否匹配。

我遇到的就是第二种情况. 报错代码:

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
            }           
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值