如何在 Swift 5.2- Xcode 版本 11.5 上覆盖 func observeValueForKeyPath

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

我正在尝试将 this interactive playground 移植到 Swift 5.2 - Xcode 版本 11.5但我仍然得到 Method does not override any method from its superclass 并且我仍然没有弄清楚如何设置此方法签名。我正在研究的 class 从 UIKit.

扩展 UIView

我用 Cmd+Shift+O 查看 header NSKeyValueObserving.h 并找到:

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

从现在开始,我尝试过但没有成功:

    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?)

关于我正在研究的 class 的更多信息

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 */

您的覆盖应如下所示。它要求你的参数是可选的。

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

每当发生这种情况时,请查看 CMD+SHFT+O 对该方法的定义以查看定义并验证您的参数是否符合预期。我相信这是比您的用例更新的 Swift 版本,但它仍然适用。