1

I'm trying to use ReactiveCocoa bindings to bind a UITextView's attributedText to a model. However, the attributedString value does not get saved as expected. Is something wrong with this binding?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:self.note.attributedText
                                                                      options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                                NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                           documentAttributes:nil error:&error];
    self.textTextView.attributedText = attributedText;

    RAC(self.note, attributedText) = [RACObserve(self.textTextView, attributedText) map:^id(NSAttributedString *attributedText) {
        return [attributedText dataFromRange:NSMakeRange(0, attributedText.length)
                   documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                        NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                error:nil];
    }];
tkuichooseyou
  • 650
  • 1
  • 6
  • 16
  • Is it possible that `UITextView.attributedString` [is not key/value-observable?](http://stackoverflow.com/questions/6039309/when-does-an-associated-object-get-released/6051404#6051404) – erikprice Apr 14 '15 at 21:25
  • Yes you're right, it is not key/value observable according to this thread: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/1090 Is there a different way to achieve this binding for attributedText then? – tkuichooseyou Apr 14 '15 at 21:46
  • Not using `RACObserve()`, no. I suppose you could either poll the property you want to observe (yuck), or use method swizzling to replace it with an implementation that notifies you when it's given a new value (also yuck), but I don't believe ReactiveCocoa has anything built-in to support those ideas. – erikprice Apr 17 '15 at 02:27

1 Answers1

0

I believe that you can use the rac_textSignal category here. Have you tried something like the following?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:self.note.attributedText
                                                                      options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                                NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                           documentAttributes:nil error:&error];
    self.textTextView.attributedText = attributedText;

    @weakify(self);
    RAC(self.note, attributedText) = [self.textTextView.rac_textSignal map:^id(__unused NSString *text) {
        @strongify(self);
        return [self.textTextView.attributedText dataFromRange:NSMakeRange(0, attributedText.length)
                   documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                        NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                error:nil];
    }];
}
Ray Lillywhite
  • 746
  • 5
  • 12