Skip to content

Files

Latest commit

 

History

History
36 lines (25 loc) · 948 Bytes

block_based_kvo.md

File metadata and controls

36 lines (25 loc) · 948 Bytes

Pattern: Missing use of block based KVO API

Issue: -

Description

Prefer the new block based KVO API with keypaths when using Swift 3.2 or later.

Examples of correct code:

let observer = foo.observe(\.value, options: [.new]) { (foo, change) in
   print(change.newValue)
}

Examples of incorrect code:

class Foo: NSObject {
   override func observeValue(forKeyPath keyPath: String?, of object: Any?,
                               change: [NSKeyValueChangeKey : Any]?,
                               context: UnsafeMutableRawPointer?) {}
}


class Foo: NSObject {
   overridefunc observeValue(forKeyPath keyPath: String?, of object: Any?,
                               change: Dictionary<NSKeyValueChangeKey, Any>?,
                               context: UnsafeMutableRawPointer?) {}
}

Further Reading