12

I’ve SwiftLint enabled in project and it throws an warning for below function :

override func observeValue(forKeyPath keyPath: String?, of _: Any?, change: [NSKeyValueChangeKey: Any]?, context _: UnsafeMutableRawPointer?) {
    . . .
}

Screenshot

Shell Script Invocation Warning : Block Based KVO Violation : Prefer the new block based KVO API with key paths when using Swift 3.2 or later.

Any fix for this?

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177

1 Answers1

10

A good tutorial to make Block Base KVO Here

class CounterModel : NSObject {

    @objc dynamic var value = 0
    @objc dynamic var messages = [String]()

}


model.observe(\.value, options: [.initial]) { (model, change) in
    self.label.text = String(model.value)
}
dimohamdy
  • 2,917
  • 30
  • 28