forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDefaultsObserver.swift
More file actions
36 lines (31 loc) · 894 Bytes
/
UserDefaultsObserver.swift
File metadata and controls
36 lines (31 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Foundation
public final class UserDefaultsObserver: NSObject {
public var onChange: (() -> Void)?
private weak var object: NSObject?
private let keyPaths: [String]
public init(
object: NSObject,
forKeyPaths keyPaths: [String],
context: UnsafeMutableRawPointer?
) {
self.object = object
self.keyPaths = keyPaths
super.init()
for keyPath in keyPaths {
object.addObserver(self, forKeyPath: keyPath, options: .new, context: context)
}
}
deinit {
for keyPath in keyPaths {
object?.removeObserver(self, forKeyPath: keyPath)
}
}
public override func observeValue(
forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey: Any]?,
context: UnsafeMutableRawPointer?
) {
onChange?()
}
}