Application termination due to uncaught exception "NSRangeException", reason: "Unable to delete observer - ios

I am developing an ios application using swift. I am using xcode7.0.1. with TableViewController. I want to expand when I click a line and it crashes when I click it again. I am following a tutorial from gitHub . Now i ran into an errorTerminating 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 the next line of code will cause a problem.

Code of my UITableViewCell:

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. Hope this should be a simple problem. Please help me solve this problem.

I do not know what details need to be published here. Please comment if I want to add more details.

+4
2

@Leandros . .

, :

 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()
    }
+11

KVO , .

, , . , , (Objective-C):

@try {
    [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(isFinished))];
} @catch (NSException * __unused exception) {}

Swift, Swift 2.1, try {} catch {}. , 2.1 .

: Swift 2 do/try/catch, , Objective-C, ( Swift 2.1 Xcode 7.1) Swift NSExceptions, , Objective-C @catch, Swift.

KVO , , .

+5

Source: https://habr.com/ru/post/1609907/


All Articles