有错误的 RACSignal 映射

RACSignal map with error

我有一个简单的问题,但找不到好的解决方案。

我有一个发送字符串的信号,然后是 mapmap 将字符串转换为 JSON。

可能会出现字符串格式错误,JSON解析器解析失败并报错的情况。

[stringGeneratorSignal map:^(NSString *bussinessObjectString){
    NSError *error;
    BussinessObject *obj = [[BussinessObject alloc] initWithString:bussinessObjectString error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    return obj;
}];

但是因为我在地图里面,所以我不能return错误信号。我想要的是得到解析器提供的错误的错误。

我分析了一些我不喜欢的可能性:

这种情况的最佳方法是什么?

谢谢!

查看-tryMap。它允许您 return 数据或 nil 然后设置错误

/// Runs `mapBlock` against each of the receiver's values, mapping values until
/// `mapBlock` returns nil, or the receiver completes.
///
/// mapBlock - An action to map each of the receiver's values. The block should
///            return a non-nil value to indicate that the action was successful.
///            This block must not be nil.
///
/// Example:
///
///   // The returned signal will send an error if data cannot be read from
///   // `fileURL`.
///   [signal tryMap:^(NSURL *fileURL, NSError **errorPtr) {
///       return [NSData dataWithContentsOfURL:fileURL options:0 error:errorPtr];
///   }];
///
/// Returns a signal which transforms all the values of the receiver. If
/// `mapBlock` returns nil for any value, the returned signal will error using
/// the `NSError` passed out from the block.
- (RACSignal *)tryMap:(id (^)(id value, NSError **errorPtr))mapBlock;