Rac 매크로 작업

4104 단어
keypath
매개 변수뒤에 있는 하위 문자열입니다.없으면문자열 자체의 두 번째 인자를 취할 때 두 번째 인자의 문자열을 가져옵니다.두 개의 매개 변수, 첫 번째 매개 변수의 역할은 자동으로 OBJ를 보완하는 데 있다.PATH, 코드 자동 완성

define keypath(...) \


metamacro_if_eq(1, metamacro_argcount(VA_ARGS))(keypath1(VA_ARGS))(keypath2(VA_ARGS))

define keypath1(PATH) \


(((void)(NO && ((void)PATH, NO)), strchr(# PATH, '.') + 1))

define keypath2(OBJ, PATH) \


(((void)(NO && ((void)OBJ.PATH, NO)), # PATH))
keypath(self);      //(((void)(__objc_no && ((void)self, __objc_no)), strchr("self", '.') + 1)) ;
keypath(self.view);     // (((void)(__objc_no && ((void)self.view, __objc_no)), strchr("self.view", '.') + 1)) ;
keypath(self.view.backgroundColor);  //(((void)(__objc_no && ((void)self.view.backgroundColor, __objc_no)), strchr("self.view.backgroundColor", '.') + 1)) ;


keypath(self, view.backgroundColor);     //   (((void)(__objc_no && ((void)self.view.backgroundColor, __objc_no)), "view.backgroundColor"));
keypath(self.view,backgroundColor);     //    (((void)(__objc_no && ((void)self.view.backgroundColor, __objc_no)), "backgroundColor"));
keypath(self.view.backgroundColor,CGColor); //(((void)(__objc_no && ((void)self.view.backgroundColor.CGColor, __objc_no)), "CGColor"));

1、 RACObserve
RACObserve(self, kvoValue);

({ __attribute__((objc_ownership(weak))) id target_ = (self);

[target_ rac_valuesForKeyPath:@(((void)(__objc_no && ((void)self.kvoValue, __objc_no)), "kvoValue")) observer:self]; });
  • (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(__weak NSObject *)observer { return [[[self rac_valuesAndChangesForKeyPath:keyPath options:NSKeyValueObservingOptionInitial observer:observer] map:^(RACTuple *value) {//-map: because it doesn't require the block trampoline that -reduceEach: uses return value[0]; }] setNameWithFormat:@"RACObserve(%@, %@)", RACDescription(self), keyPath]; }

  • 2、 RACChannelTo RACChannelTo(self.view, backgroundColor) = RACChannelTo(self,sscolor);
    [[RACKVOChannel alloc] initWithTarget:(self.view) keyPath:
    

    @(((void)(__objc_no && ((void)self.view.backgroundColor, __objc_no)), "backgroundColor")) nilValue:(((void *)0))] [@(((void)(__objc_no && ((void)RACKVOChannel.new.followingTerminal, __objc_no)), "followingTerminal"))]
    = [[RACKVOChannel alloc] initWithTarget:(self) keyPath: @(((void)(__objc_no && ((void)self.sscolor, __objc_no)), "sscolor")) nilValue:(((void *)0))]
    [@(((void)(__objc_no && ((void)RACKVOChannel.new.followingTerminal, __objc_no)), "followingTerminal"))] ;
    //아래 첨자 작업
    @implementation RACKVOChannel (RACChannelTo)
  • (RACChannelTerminal *)objectForKeyedSubscript:(NSString *)key { NSCParameterAssert(key != nil); RACChannelTerminal *terminal = [self valueForKey:key]; NSCAssert([terminal isKindOfClass:RACChannelTerminal.class], @"Key "%@"does not identify a channel terminal", key); return terminal; }
  • (void)setObject:(RACChannelTerminal *)otherTerminal forKeyedSubscript:(NSString *)key { NSCParameterAssert(otherTerminal != nil); RACChannelTerminal *selfTerminal = [self objectForKeyedSubscript:key]; [otherTerminal subscribe:selfTerminal]; [[selfTerminal skip:1] subscribe:otherTerminal]; }

  • 3、 RAC
    [[RACSubscriptingAssignmentTrampoline alloc] initWithTarget:(TARGET) nilValue:(NILVALUE)][@keypath(TARGET, KEYPATH)]
    
    
    
     [[RACSubscriptingAssignmentTrampoline alloc] initWithTarget:(self.view) nilValue:([UIColor redColor])]
    

    [@(((void)(__objc_no && ((void)self.view.backgroundColor, __objc_no)),
    "backgroundColor"))]=a;
    //아래 첨자 작업
  • (void)setObject:(RACSignal *)signal forKeyedSubscript:(NSString *)keyPath { [signal setKeyPath:keyPath onObject:self.target nilValue:self.nilValue]; }
  • 좋은 웹페이지 즐겨찾기