ReactiveCocoa5.0 &ReactiveSwift &Reactive.Swift (Base)理解

这里写图片描述

源码

/// 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 所代表的类型*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值