Search code examples
cocoadata-bindingcocoa-bindings

What's wrong with my Shared User Defaults Controller binding?


I have created two dictionaries in the user defaults of my application to represent the controls of two players, as such:

Player1 = {
    MoveLeft = 0;
    MoveRight = 2;
    MoveDown = 1;
,
Player2 = {
    MoveLeft = 40;
    MoveRight = 41;
    MoveDown = 37;
}

And when I access [NSUserDefaults.standardUserDefaults valueForKeyPath:@"Player2.MoveLeft"], I do get a NSNumber with 40.

I am trying to bind several text fields to these values with the regular bindings mechanism. When I select the text field, this is what shows up:

Bind To = Shared User Defaults Controller, Controller Key = value, Model Key Path = Player2.MoveLeft, Value Transformer = SRKeyCodeTransformer

However, the text field is empty when I run the application. (Even if I remove the value transformer.) If I try to bind to a top-level property (like if I define foo in the defaults with a value of 40), it works.

What's wrong with my binding? How can I access an element from a nested dictionary?


Solution

  • I don't remember where exactly, but I found my answer: you cannot bind to non-top-level objects in the user defaults because the collections are immutable.