sdasdadas
sdasdadas

Reputation: 25096

MutableProperty containing a dictionary in ReactiveSwift

I am using ReactiveSwift to create a struct containing a dictionary. I want to listen for changes in the dictionary.

struct Model {
    let a: MutableProperty<[String: Int]> = MutableProperty([:])
}

However, I'm having a hard time understanding how to bind this property to a listener. I want to do something like:

textView.reactive.text <~ model.a["key"]

Is there a solution to holding dictionaries in mutable properties?

Upvotes: 2

Views: 576

Answers (2)

biobod
biobod

Reputation: 11

Or you can do it like this:

textView.reactive.text <~ model.a.map { $0["key"] }

Upvotes: 1

tkuichooseyou
tkuichooseyou

Reputation: 650

Only the MutableProperty associated value (in your case, the dictionary) is able to be binded to the binding target, and not a value within the dictionary. That means you can't use the <~ operator on a value from the dictionary. You'll need to do something like:

model.a.producer.startWithValues { [weak textView] value in
     textView?.text = value["key"]
}

Upvotes: 3

Related Questions