Amsheer Amsheer - 4 months ago 43
Swift Question

Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer - ios

I am developing ios app by using swift. I am using xcode7.0.1. with TableViewController. I want to expand when i click row and collapse when i click it again. I am following the tutorial from gitHub. Now i am facing the error

Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer
for the key path "frame" from <X.expandTips 0x145d8d20> because it is not registered as an observer.'


I hope following line of code will causing the issue.

My UITableViewCell class code:

func checkHeight(){
expandaple_view.hidden = (frame.size.height < expandTips.expandedHeight)
}

func WatchFrameChanges() {
addObserver(self , forKeyPath: "frame", options: .New, context: nil )
checkHeight()
}

func ignoreFrameChanges(){

removeObserver(self, forKeyPath: "frame")

}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "frame"{
checkHeight()
}
}


And in my TableViewController code:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! expandTips) . WatchFrameChanges()
}

override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! expandTips) . ignoreFrameChanges()
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath == selectedIndexPath ){
return expandTips.expandedHeight
}else {
return expandTips.defaultHeight
}
}


I am new to ios. I hope it must be simple issue. Please someone help me to solve this.

I don't know what are the details needs to post here. Please comment if i want to add more details.

Answer

From the comment of @Leandros I found the solution. Just tracking the observer using boolean variable.

Use of following code, I found the solution:

 var frameAdded = false
 func checkHeight(){

        expanding_view.hidden = (frame.size.height < expandTips.expandedHeight)
    }

    func WatchFrameChanges() {
        if(!frameAdded){
        addObserver(self , forKeyPath: "frame", options: .New, context: nil )
            frameAdded = true
        }
    }

    func ignoreFrameChanges() {
          if(frameAdded){
        removeObserver(self, forKeyPath: "frame")
            frameAdded = false
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "frame"{
            checkHeight()
        }
    }
    deinit {
        print("deinit called");
        ignoreFrameChanges()
    }