Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 9 : Block Based KVO Violation for observeValue function

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?

like image 434
Jayprakash Dubey Avatar asked Apr 10 '18 07:04

Jayprakash Dubey


1 Answers

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)
}
like image 186
dimohamdy Avatar answered Nov 15 '22 19:11

dimohamdy