Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very basic TextField/TextView two way binding to viewModel #3093

Closed
tunidev opened this issue Jul 28, 2016 · 3 comments
Closed

Very basic TextField/TextView two way binding to viewModel #3093

tunidev opened this issue Jul 28, 2016 · 3 comments
Labels

Comments

@tunidev
Copy link

tunidev commented Jul 28, 2016

I have a textfield and a textView in my UIViewController and in my ViewModel I have two String variable, I want to bind the textField to variabletitle and the textView to variable textBody. In RAC2 it's straight forward with RAC but what about RAC4 and Swift2 ?

I found this in StackOverFlow :

        let producer = textField.rac_textSignal().toSignalProducer()
            .flatMapError { error in
                return SignalProducer<AnyObject?, NoError>.empty
            }
            .map { text in Int(text as! String) }

        self.viewModel.title <~ producer

but I get Use of undeclared type 'NoError' so I changed it to NSError but now I get : Binary operator '<~' cannot be applied to operands of type 'String' and 'SignalProducer<Int?, NSError>' (aka 'SignalProducer<Optional<Int>, NSError>')

self.viewModel.title is a simple var title = ""

I know that I can do that :

        textField.rac_textSignal()
            .toSignalProducer()
            .map { text in text as! String } // AnyObject -> String
            .filter { string in string.characters.count > 0 } // String -> Bool
            .startWithNext { [weak self] (text) in
                self?.viewModel.title = text
        }

but why all that code to just bind and the use of startWithNext and [weak self] :(

@andersio
Copy link
Member

andersio commented Jul 29, 2016

Swift does not have macro or KVC in objects, so there is no binding magic like RAC(). Bindings in Swift are intended to target Property types.

P.S. You may need import enum Result.NoError for the NoError type.

@mdiep mdiep added the question label Jul 29, 2016
@andersio
Copy link
Member

andersio commented Oct 22, 2016

In ReactiveCocoa 5.0, primitives like reactive.text and reactive.textValues are provided in various UIKit/AppKit controls and views.

textField.text <~ viewModel.title.producer
viewModel.title <~ textField.textValues

Although we do not have a bidirectional binding system in ReactiveCocoa yet, the above snippet would work without an infinite feedback loop.

This only works for bindings with controls though. Bidirectional bindings between properties, if you ever need it, are impossible without a mediator in between.

@lukaskubanek
Copy link

lukaskubanek commented Jun 10, 2020

@andersio I’ve seen that you started the work on support for bidirectional bindings on MutableProperty in ReactiveCocoa/ReactiveSwift#181 and ReactiveCocoa/ReactiveSwift#182 a couple of years ago. Are there any plans to continue at some point, or is this kind of binding out of scope for ReactiveCocoa/ReactiveSwift? I’m just curious to hear an update on that since I run into this problem quite often, and it always brings me to this GitHub issue. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants