ReactiveObjCBridge
在 Swift 发布后,ReactiveCocoa 重新用 Swift 编写了。这个框架在 Swift 和 Objective-C API 之间创建了一个桥梁(分别为 ReactiveSwift 和 ReactiveObjC)。
由于 API 基于完全不同的设计,转换并不总是单一的;然而,已经尽力在两个 API(和语言)之间忠实转换概念。
桥接类型包括
RACSignal和SignalProducer或SignalRACCommand和ActionRACScheduler和SchedulerTypeRACDisposable和Disposable
有关完整的桥接 API(包括文档),请参阅 ObjectiveCBridging.swift。
RACSignal 和 SignalProducer 或 Signal
在 ReactiveSwift 中,“冷”信号由 SignalProducer 类型表示,“热”信号由 Signal 类型表示。
可以将“冷”RACSignal 转换为 SignalProducer,使用 SignalProducer 初始化器
extension SignalProducer where Error == Swift.Error {
public init<SignalValue>(_ signal: RACSignal<SignalValue>) where Value == SignalValue?
}“热”RACSignal 不能直接转换为 Signal,因为任何 RACSignal 订阅都可能涉及副作用。为了获得一个 Signal,请使用 RACSignal.toSignalProducer 后跟 SignalProducer.start,这将使潜在的副作用明确化。
对于相反的操作,请使用bridged属性。
在调用SignalProducer时,这些函数将为每个订阅创建一个RACSignal以一次启动生产者。
extension SignalProducerProtocol where Value: AnyObject {
public var bridged: RACSignal<Value>
}
extension SignalProducerProtocol where Value: OptionalProtocol, Value.Wrapped: AnyObject {
public var bridged: RACSignal<Value.Wrapped>
}
在调用Signal时,这些方法将创建一个简单地观察它的RACSignal。
extension SignalProtocol where Value: AnyObject {
public var bridged: RACSignal<Value.Wrapped> {
}
extension SignalProtocol where Value: OptionalProtocol, Value.Wrapped: AnyObject {
public var bridged: RACSignal<Value.Wrapped> {
}通过特殊的初始化器init(bridging:),可以桥接带有编号元组的RACSignal到Swift元组的SignalProducer
extension SignalProducer where Error == Swift.Error {
public init<First>(bridging tupleSignal: RACSignal<RACOneTuple<First>>) where Value == First?
public init<First, Second>(bridging tupleSignal: RACSignal<RACTwoTuple<First, Second>>) where Value == (First?, Second?)?
public init<First, Second, Third>(bridging tupleSignal: RACSignal<RACThreeTuple<First, Second, Third>>) where Value == (First?, Second?, Third?)?
public init<First, Second, Third, Fourth>(bridging tupleSignal: RACSignal<RACFourTuple<First, Second, Third, Fourth>>) where Value == (First?, Second?, Third?, Fourth?)?
public init<First, Second, Third, Fourth, Fifth>(bridging tupleSignal: RACSignal<RACFiveTuple<First, Second, Third, Fourth, Fifth>>) where Value == (First?, Second?, Third?, Fourth?, Fifth?)?
}RACCommand和Action
要将RACCommand转换为新的Action类型,请使用Action初始化器
extension Action where Error == Swift.Error {
public convenience init<CommandInput, CommandOutput>(
_ command: RACCommand<CommandInput, CommandOutput>
) where Input == CommandInput?, Output == CommandOutput?
}要将Action转换为RACCommand,请使用bridged实例方法
extension Action where Input: AnyObject, Output: AnyObject {
public var bridged: RACCommand<Input, Output>
}
extension Action where Input: OptionalProtocol, Input.Wrapped: AnyObject, Output: AnyObject {
public var bridged: RACCommand<Input.Wrapped, Output>
}
extension Action where Input: AnyObject, Output: OptionalProtocol, Output.Wrapped: AnyObject {
public var bridged: RACCommand<Input, Output.Wrapped>
}
extension Action where Input: OptionalProtocol, Input.Wrapped: AnyObject, Output: OptionalProtocol, Output.Wrapped: AnyObject {
public var bridged: RACCommand<Input.Wrapped, Output.Wrapped>
}注意:动作和命令的executing属性在API桥梁上不是同步的。为了确保一致性,只从基础对象(传入桥梁的对象,而不是从桥梁中检索的对象)观察executing属性,无论使用哪个对象执行,都会发生更新。
RACScheduler和SchedulerType
任何RACScheduler实例都是自动的DateSchedulerType(因此也是SchedulerType),可以直接传递给任何期望它的函数或方法。
所有Scheduler和DateScheduler都可以使用RACScheduler初始化器进行包装
extension RACScheduler {
public convenience init(_ scheduler: Scheduler)
public convenience init(_ scheduler: DateScheduler)
}请注意,包装的Scheduler在延迟安排方法使用时将表现为RACImmediateScheduler
RACDisposable和Disposable
任何RACDisposable实例都是自动的Disposable,可以直接在任何期望类型符合Disposable的地方使用。
使用RACDisposable初始化器来包装Disposable的实例
extension RACDisposable {
public convenience init(_ disposable: Disposable?)
}编号的RACTuple
编号(和类型化)的RACTuple子类型,如RACOneTuple、RACTwoTuple等,可以通过一系列全局函数桥接到本地Swift元组。
public func bridgedTuple<First>(from tuple: RACOneTuple<First>) -> (First?)
public func bridgedTuple<First, Second>(from tuple: RACTwoTuple<First, Second>) -> (First?, Second?)
public func bridgedTuple<First, Second, Third>(from tuple: RACThreeTuple<First, Second, Third>) -> (First?, Second?, Third?)
public func bridgedTuple<First, Second, Third, Fourth>(from tuple: RACFourTuple<First, Second, Third, Fourth>) -> (First?, Second?, Third?, Fourth?)
public func bridgedTuple<First, Second, Third, Fourth, Fifth>(from tuple: RACFiveTuple<First, Second, Third, Fourth, Fifth>) -> (First?, Second?, Third?, Fourth?, Fifth?)