RAC源码解析之关键词作为消息名称

2018-07-17  本文已影响11人  传说中的汽水枪

初次看到这样的代码:

        defer:^{
            @strongify(self);
            return [RACSignal return:self];
        }]

整个人都惊讶了,return居然有这种用法?上网一查然后没有任何方面介绍此用法,最后只能理解为这个return是一个方法名,而且是一个类的方法名
全局搜索相关的:


搜索:+ (__kindof RACStream<ValueType> *)return:(nullable ValueType)value
搜索:+ (__kindof RACStream *)return:(id)value
搜索:(RACSignal *)return:(id)value

理解里面的return代码:
RACSignal.m

+ (RACSignal *)return:(id)value {
    return [RACReturnSignal return:value];
}

RACReturnSignal.m


+ (RACSignal *)return:(id)value {
#ifndef DEBUG
    // In release builds, use singletons for two very common cases.
    if (value == RACUnit.defaultUnit) {
        static RACReturnSignal *unitSingleton;
        static dispatch_once_t unitPred;
        dispatch_once(&unitPred, ^{
            unitSingleton = [[self alloc] init];
            unitSingleton->_value = RACUnit.defaultUnit;
        });
        return unitSingleton;
    } else if (value == nil) {
        static RACReturnSignal *nilSingleton;
        static dispatch_once_t nilPred;
        dispatch_once(&nilPred, ^{
            nilSingleton = [[self alloc] init];
            nilSingleton->_value = nil;
        });
        return nilSingleton;
    }
#endif
    RACReturnSignal *signal = [[self alloc] init];
    signal->_value = value;
#ifdef DEBUG
    [signal setNameWithFormat:@"+return: %@", value];
#endif

    return signal;
}

这些关键字怎么有这种用法。。。只能说崩溃了。
我也测试了相关的其他关键词:


@interface RXOCKeywordMethodNameObject : NSObject

@end

@implementation RXOCKeywordMethodNameObject

+ (id)return
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)return
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}

+ (id)void
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)void
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}
+ (id)id
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)id
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}

+ (id)weak
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)weak
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}

+ (id)strong
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)strong
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}


+ (id)readonly
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)readonly
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}

+ (id)self
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)self
{
    // 有一个warning, 目前还不知道怎么解决
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}

+ (id)super
{
    return [NSString stringWithFormat:@"class method name: %@", NSStringFromSelector(_cmd)];
}
- (id)super
{
    return [NSString stringWithFormat:@"instance method name: %@", NSStringFromSelector(_cmd)];
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    RXOCKeywordMethodNameObject *object = [RXOCKeywordMethodNameObject new];
    
    NSLog(@"%@", [object return]);
    NSLog(@"%@", [object void]);
    NSLog(@"%@", [object id]);
    NSLog(@"%@", [object weak]);
    NSLog(@"%@", [object strong]);
    NSLog(@"%@", [object self]);
    NSLog(@"%@", [object super]);
    
    NSLog(@"%@", [RXOCKeywordMethodNameObject return]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject void]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject id]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject weak]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject strong]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject self]);
    NSLog(@"%@", [RXOCKeywordMethodNameObject super]);
}

都可以得到正确的结果。

所以RAC里面还有这样的代码:

+ (RACSignal<ValueType> *)switch:(RACSignal *)signal cases:(NSDictionary *)cases default:(nullable RACSignal *)defaultSignal RAC_WARN_UNUSED_RESULT;
+ (RACSignal<ValueType> *)if:(RACSignal<NSNumber *> *)boolSignal then:(RACSignal *)trueSignal else:(RACSignal *)falseSignal RAC_WARN_UNUSED_RESULT;
上一篇下一篇

猜你喜欢

热点阅读