ReactiveCocoa
发布时间:2020-12-15 05:27:40 所属栏目:百科 来源:网络整理
导读:那我们来看一看这最简单的例子, // When self.username changes,logs the new name to the console.//// RACObserve(self,username) creates a new RACSignal that sends the current// value of self.username,then the new value whenever it changes.//
那我们来看一看这最简单的例子, // When self.username changes,logs the new name to the console. // // RACObserve(self,username) creates a new RACSignal that sends the current // value of self.username,then the new value whenever it changes. // -subscribeNext: will execute the block whenever the signal sends a value. [RACObserve(self,username) subscribeNext:^(NSString *newName) { NSLog(@"%@",newName); }]; 上面这段代码的作用是,当username发生变化的时候打印username。 接下来,看看内部具体的代码 NSObject+RACPropertySubscribing.h #define RACObserve(TARGET,KEYPATH) [(id)(TARGET) rac_valuesForKeyPath:@keypath(TARGET,KEYPATH) observer:self] - (RACSignal *)rac_valuesFor KeyPath:(NSString *)keyPath observer:(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(%@,%@)",self.rac_description,keyPath]; } - (RACSignal *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(NSObject *)observer { return [self rac_observeKeyPath:keyPath options:options observer:observer block:^(id value,NSDictionary *change,BOOL causedByDealloc,BOOL affectedOnlyLastComponent) { [subscriber sendNext:RACTuplePack(value,change)]; }]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |