Created
January 15, 2015 10:54
-
-
Save akisute/821513ea4e4fa688b745 to your computer and use it in GitHub Desktop.
How to use RACObserve in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Objective-C | |
@interface Document: NSObject | |
@property (nonatomic, copy) NSArray *notes; | |
@property (nonatomic, readonly) RACSignal *notesUpdatedSignal; | |
@end | |
@implementation Document | |
- (RACSignal *)notesUpdatedSignal | |
{ | |
// In Objective-C, just simply use RACObserve | |
return RACObserve(self, notes); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift | |
// 1. Do not forget to inherit NSObject (or add @objc attribute) | |
public class Document: NSObject { | |
// 2. Make sure to add 'dynamic' modifier | |
public dynamic var notes: [Note]= [] | |
// 3. observer should always be 'self' | |
public var notesUpdatedSignal: RACSignal { | |
return self.rac_valuesForKeyPath("notes", observer: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment