杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
//  NSObject+Rx+KVORepresentable.swift
//  RxCocoa
//
//  Created by Krunoslav Zaher on 11/14/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
 
#if !os(Linux)
 
import Foundation
import RxSwift
 
/// Key value observing options
public struct KeyValueObservingOptions: OptionSet {
    /// Raw value
    public let rawValue: UInt
 
    public init(rawValue: UInt) {
        self.rawValue = rawValue
    }
 
    /// Whether a sequence element should be sent to the observer immediately, before the subscribe method even returns.
    public static let initial = KeyValueObservingOptions(rawValue: 1 << 0)
    /// Whether to send updated values.
    public static let new = KeyValueObservingOptions(rawValue: 1 << 1)
}
 
extension Reactive where Base: NSObject {
 
    /**
     Specialization of generic `observe` method.
 
     This is a special overload because to observe values of some type (for example `Int`), first values of KVO type
     need to be observed (`NSNumber`), and then converted to result type.
 
     For more information take a look at `observe` method.
     */
    public func observe<Element: KVORepresentable>(_ type: Element.Type, _ keyPath: String, options: KeyValueObservingOptions = [.new, .initial], retainSelf: Bool = true) -> Observable<Element?> {
        return self.observe(Element.KVOType.self, keyPath, options: options, retainSelf: retainSelf)
            .map(Element.init)
    }
}
 
#if !DISABLE_SWIZZLING && !os(Linux)
    // KVO
    extension Reactive where Base: NSObject {
        /**
        Specialization of generic `observeWeakly` method.
 
        For more information take a look at `observeWeakly` method.
        */
        public func observeWeakly<Element: KVORepresentable>(_ type: Element.Type, _ keyPath: String, options: KeyValueObservingOptions = [.new, .initial]) -> Observable<Element?> {
            return self.observeWeakly(Element.KVOType.self, keyPath, options: options)
                .map(Element.init)
        }
    }
#endif
 
#endif