借用Stack Overflow例子atom
@interface Node : NSObject @property (nonatomic, assign) BOOL detached; @end
@implementation Node - (void)setParentNode:(Node *)parentNode { _parentNode = parentNode; RAC(self, detached) = [RACObserve(_parentNode, detached) filter:^BOOL(id value) { return [value boolValue]; }]; } @end
錯誤一: 儘可能不要在setter方法中構造信號,由於有肯能get 或者 set 方法會被屢次調用,有可能會引發內存泄漏或者其餘(我的猜想)code
在init方法中進行監聽blog
- (id)init { self = [super init]; if (self == nil) return nil; // Use @NO instead if that makes more sense as a default. // @YES至關於設置默認爲YES RAC(self, detached, @YES) = [RACObserve(self, parentNode.detached) filter:^(NSNumber *detached) { return [detached boolValue]; }]; return self; }
原文連接內存