ReactiveCocoa5.0 &ReactiveSwift &Reactive.Swift (Base)理解
程序员文章站
2022-04-11 16:11:33
...
源码
/// Describes a provider of reactive extensions.
///
/// - note: `ReactiveExtensionsProvider` does not indicate whether a type is
/// reactive. It is intended for extensions to types that are not owned
/// by the module in order to avoid name collisions and return type
/// ambiguities.
public protocol ReactiveExtensionsProvider: class {}
extension ReactiveExtensionsProvider {
/// A proxy which hosts reactive extensions for `self`.
public var reactive: Reactive<Self> {
return Reactive(self)
}
/// A proxy which hosts static reactive extensions for the type of `self`.
public static var reactive: Reactive<Self>.Type {
return Reactive<Self>.self
}
}
/// A proxy which hosts reactive extensions of `Base`.
public struct Reactive<Base> {
/// The `Base` instance the extensions would be invoked with.
public let base: Base
/// Construct a proxy
///
/// - parameters:
/// - base: The object to be proxied.
fileprivate init(_ base: Base) {
self.base = base
}
}
对Base自己的理解 不知道对不对
上面的代码简短 但是功能强大 所以拿出来分析一下
先来看看struct
定义的一个结构体 但是类型是Base 你会发现Swift里是没有这个类型的 ,其实奥秘就在Reactive中的那个
* 不同之处在于这个范型结构体后面跟着的占位类型名称 Base 并且是用<>包围修饰的 ,这个<> 告诉Swift 那个Base 是 Reactive结构体内的一个占位类型名 因此 Swift是不会去查找这个名为Base的实际类型 只有在初始化Reactive的时候,才能根据所传入的实际类型决定 Base 所代表的类型*