Swift 4 approach for observeValue(forKeyPath:…)

前端 未结 2 904
轮回少年
轮回少年 2021-01-30 13:26

I\'ve been trying to find an example, but what I\'ve seen doesn\'t work in my case.

What would be the equivalent of the following code:

object.addObserv         


        
2条回答
  •  梦谈多话
    2021-01-30 13:29

    To add something to the answer as I experienced crashes on my app when using this method in iOS 10.

    In iOS 10, you still need to remove the observer before deallocating the class or otherwise you will get a crash NSInternalInconsistencyException stating that:

    An instance A of Class C was deallocated while key value observers were still registered with it.

    To avoid this crash. Simply set the observer property that you're using to nil.

    deinit {
        self.observation = nil
    }    
    

提交回复
热议问题