1. 程式人生 > >ReactiveCocoa 學習筆記十二(RACEvent)

ReactiveCocoa 學習筆記十二(RACEvent)

RACEvent

RACEvent 是 ReactiveCocoa 框架中用來表示訊號流所傳送的事件資訊的類,同三種訊號量相對應,該類可以分為三種。

typedef NS_ENUM(NSUInteger, RACEventType) {
    RACEventTypeCompleted,
    RACEventTypeError,
    RACEventTypeNext
};

該類提供了三個類方法,可以很方便的建立上面三種類型的事件。

+ (RACEvent<ValueType> *)completedEvent;


+ (RACEvent<ValueType> *)eventWithError:(nullable NSError *)error;


+ (RACEvent<ValueType> *)eventWithValue:(nullable ValueType)value;

completedEvent 方法會返回一個共享的例項物件,並且這三個方法都呼叫了同一個方法 initWithEventType:object:

提供了一個屬性表示事件型別

@property (nonatomic, assign, readonly) RACEventType eventType;

提供了一個屬性表示事件是否結束,即該事件型別是否是 RACEventTypeError 或 RACEventTypeCompleted 。

@property (nonatomic, getter = isFinished, assign, readonly) BOOL finished;

提供了一個屬性表示同事件型別為 RACEventTypeError 的事件相關聯的錯誤資訊

@property (nonatomic, strong, readonly, nullable) NSError *error;

提供了一個屬性表示同事件型別為 RACEventTypeNext 的事件相關聯的值

@property (nonatomic, strong, readonly, nullable) ValueType value;

error 和 value 實際都是由內部屬性 object 儲存的。