Skip to content

Instantly share code, notes, and snippets.

@akisute
Created January 15, 2015 10:54
Show Gist options
  • Save akisute/821513ea4e4fa688b745 to your computer and use it in GitHub Desktop.
Save akisute/821513ea4e4fa688b745 to your computer and use it in GitHub Desktop.
How to use RACObserve in Swift
// 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
// 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