0

this new existing project is using Objective-C Code with RACObserve(Reactive cocoa ) to read api responses. I want to convert obj-c to swift .

Current Obj-c Implementation is :

@interface ObjCTableViewCell : UITableViewCell
@property (nonatomic, strong) OfferPersonal *offer;

@end

.m class->

- (void)bindToModel {

    [RACObserve(self, offer.lender.name) subscribeNext:^(id x) {
        self.nameLabel.text = x;
    }];
}

Controller ->

{
ObjCTableViewCell TableViewCell *myCell;
        myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        [myCell setValue:self.offer forKey:@"offerPersonal"];

        [myCell bindToModel];
}

Now how to use RACObserve in Swift. i tried to search it over other places. (http://blog.scottlogic.com/2014/07/24/mvvm-reactivecocoa-swift.html)could not understand exactly .

sulabh
  • 249
  • 5
  • 22

1 Answers1

0

Is the new project using ReactiveSwift with everything (model, cell, view controller) being rewritten in Swift? If not, you can use ReactiveObjcBridge to call the old ReactiveCocoa ObjC library from Swift.

If yes, you should use replace the name String property with a MutableProperty<String>, and you can bind to the MutableProperty - two options are:

a: the bind operator <~

nameLabel.reactive.text <~ offer.lender.name

or

b: by accessing its signal

offer.lender.name.signal.observeValues { (value: String) in nameLabel.text = value }

Also, depending on how many times the property is updated, you might want to limit the observation to only one value, like .signal.take(first: 1).observeValues { ... } or signal.take(until: reactive.prepareForReuse)

tkuichooseyou
  • 650
  • 1
  • 6
  • 16