RAC3创建Signal
项目中添加ReactiveCocoa最简单的方式是使用Carthage
,创建如下的Cartfile
即可:
github "ReactiveCocoa/ReactiveCocoa" "v3.0-beta.1"
在项目路径下执行carthage update
ReactiveCocoa3.0包含全新的Swift-API,同时也支持Objective-C,详情参考Change Log。ReactiveCocoa3.0存在两种不同类型的Signal
,一种是旧的Obj-C风格的RACSignal
,另一种是新的Swift风格的Signal
.
Swift版signal
有一个非常重要的功能是使用的泛型
class Signal<T, E: ErrorType> {
...
}
类型参数T
表示signal
发出的next
事件所带的参数类型,参数E
表示signal
发出的error
事件所带的参数类型,并且必须实现ErrorType
协议。
创建Swift版signal
和Objective-C版的方式类型。如下是创建一个信号,每秒会发送一个next
事件
func createSignal() -> Signal<String, NSError> {
var count = 0
return Signal {
sink in
NSTimer.schedule(repeatInterval: 1.0) {
timer in
sink.sendNext("tick#\(count++)")
}
return nil
}
}
Signal
的构造函数参数是一个generator:ReactiveCocoa.Observer<Value, Error>
,这里使用闭包来构造一个Observer
传入Signal
的构造函数。闭包中传入的参数就是之前传入的Observer
Signal
会将事件发送给sink
其中
NSTimer.schedule
是扩展方法,详情请见NSTimer+Closure.swift
观察Singal
有多种方式来观察或者订阅signal
。最简单的方式是使用observe
方法
下面是使用observe
观察signal
let signal = createSignal()
signal.observeNext{print($0)}
输出:
tick #0
tick #1
tick #2
tick #3
tick #4
另一种方式,可以提供一个观察signal
发送事件的对象
createSignal().observe({
event in
switch event {
case let .Next(data):
print(data)
default:
break
}
})
闭包中的event
是Event
类型,该类型是一个枚举类型
public enum Event<Value, Error : ErrorType> {
/// A value provided by the signal.
case Next(Value)
/// The signal terminated because of an error. No further events will be
/// received.
case Failed(Error)
/// The signal successfully terminated. No further events will be received.
case Completed
/// Event production on the signal has been interrupted. No further events
/// will be received.
case Interrupted
/// Whether this event indicates signal termination (i.e., that no further
/// events will be received).
public var isTerminating: Bool { get }
/// Lifts the given function over the event's value.
public func map<U>(f: Value -> U) -> ReactiveCocoa.Event<U, Error>
/// Lifts the given function over the event's error.
public func mapError<F>(f: Error -> F) -> ReactiveCocoa.Event<Value, F>
/// Unwraps the contained `Next` value.
public var value: Value? { get }
/// Unwraps the contained `Error` value.
public var error: Error? { get }
}
网友评论