Search code examples
swiftswift5xcode11swift-playgroundkey-value-observing

how to override func observeValueForKeyPath on Swift 5.2- Xcode Version 11.5


I'm trying porting this interactive playground on Swift 5.2 - Xcode Version 11.5 but I still got Method does not override any method from its superclass and I still haven't figured out how to set this method signature. The class I'm working on extends UIView from UIKit.

I looked with Cmd+Shift+O the header NSKeyValueObserving.h and found:

- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context;

Since now I've tried without success:

    public override func observeValueForKeyPath(keyPath: String!, 
    ofObject object: AnyObject!, 
    change: [String : AnyObject]!, 
    context: UnsafeMutablePointer<()>)
    
    public override func observeValueForKeyPath(keyPath: String!, 
    ofObject object: AnyObject!, 
    change: [String : AnyObject]!, 
    context: UnsafeMutableRawPointer)
    
    public override func observeValueForKeyPath(keyPath: String!, 
    of object: Any?, 
    change: [NSKeyValueChangeKey : Any]?, 
    context: UnsafeMutableRawPointer?)

more info about the class I'm working on

import UIKit
import XCPlayground

public class NewtonsCradle: UIView  {
    
    private let colors: [UIColor]
    private var balls: [UIView] = []
    
    private var animator: UIDynamicAnimator?
    private var ballsToAttachmentBehaviors: [UIView:UIAttachmentBehavior] = [:]
    private var snapBehavior: UISnapBehavior?
    
    public let collisionBehavior: UICollisionBehavior
    public let gravityBehavior: UIGravityBehavior
    public let itemBehavior: UIDynamicItemBehavior
    
    public init(colors: [UIColor]) {
        self.colors = colors
        collisionBehavior = UICollisionBehavior(items: [])
        gravityBehavior = UIGravityBehavior(items: [])
        itemBehavior = UIDynamicItemBehavior(items: [])
    
        super.init(frame: CGRect(x: 0, y: 0, width: 480, height: 320))
        backgroundColor = UIColor.white
        animator = UIDynamicAnimator(referenceView: self)
        animator?.addBehavior(collisionBehavior)
        animator?.addBehavior(gravityBehavior)
        animator?.addBehavior(itemBehavior)
        
        createBallViews()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        for ball in balls {
            ball.removeObserver(self, forKeyPath: "center")
        }
    }

   /* SNIP */

   public override func observeValueForKeyPath(keyPath: String!, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if (keyPath == "center") {
            setNeedsDisplay()
        }
    }
  /* SNIP */

Solution

  • Your overrides should look like this. It requires that your parameters are optionals.

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        // Do your work here.
    }
    

    Whenever this happens look at the definition for that method by CMD+SHFT+O to view the definition and verify that your params match what's being expected. I believe that this is a newer version of Swift than your use-case however it should still apply.