John Howard
John Howard

Reputation: 64205

Cocoa Bindings Model Key Path Division

In my project I have two text fields which are bound to a mutable dictionary. I want to make a third text field that is the first field divided by the second. However when I try to do this inside the Model Key Path in the bindings tab in Interface Builder it says it is invalid. How can I get this to work?

Upvotes: 0

Views: 1079

Answers (1)

user557219
user557219

Reputation:

You cannot perform arbitrary arithmetic operations or use arbitrary expressions in a key or key path. A key specifies a property provided by an object, and a key path specifies a sequence of properties. Although there are some variations such as collection operators, they do not apply to your particular setting.

That said, you could try:

  • Using a custom class instead of a dictionary and exposing a property that represents the arithmetic operation based on those two other properties. The Key-Value Observing Programming Guide has a section on that.

  • Using a custom value transformer that would transform the dictionary into the result of the arithmetic operation.

  • Using a category on NSDictionary to expose a property representing the arithmetic operation. I personally think this is overkill and wouldn’t really recommend it.

  • Not using bindings at all and performing the arithmetic operation directly in your application delegate, window controller, or view controller.

Upvotes: 1

Related Questions