What is difference between self.timer = nil vs [self.timer invalidate] in iOS?

前端 未结 3 1601
故里飘歌
故里飘歌 2020-12-10 14:48

Can anyone explain me self.timer=nil vs [self.timer invalidate]?

What exactly happens at the memory location of self.timer?

3条回答
  •  北海茫月
    2020-12-10 14:51

    First of all, invalidate is a method of NSTimer class which can use to stop currently running timer. Where when you assign nil to any object then, in an ARC environment the variable will release the object.

    Its important to stop running timer when you don't longer need, so we write [timer invalidate] and then we write timer = nil; to make sure it'll loose its address from memory and later time you can recreate the timer.

提交回复
热议问题