开发者

NSValueTransformer not being called

开发者 https://www.devze.com 2023-03-16 15:35 出处:网络
I have my shared user defaults plist that looks something like: menuItems (Array) Item 0 (Dictionary) name (String) \"Menu Item 0\"

I have my shared user defaults plist that looks something like:

menuItems (Array)
  Item 0 (Dictionary)
    name (String) "Menu Item 0"
    show (Boolean) NO
  Item 1 (Dictionary)
    name (String) "Menu Item 1"
    show (Boolean) YES

and so on.

I have an NSArrayController that has its content array bound to the shared user defaults controller with the Controller Key = "values" and the Model Key Path = "menuItems". I have "Handles Content As Compound Value" enabled. It's Object Controller Mode is "Class" and the Class Name it controls is NSMutableDictionary.

The desired outcome is for NSMenuItems "hidden" property to be bound to the "show" entries in the plist. You'll notice, though, that "hidden" and "show" are opposites, so I created an NSValueTransformer that simply does:

return [NSNumber numberWithBool:!value];
开发者_如何学Go

I set up the binding as follows, where "item" is an NSMenuItem, "valueTransformer" is an alloc'ed and init'ed transformer as described above, and paneNum is the index where the proper dictionary can be found for the corresponding menu item:

NSDictionary *bindingOptions = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSContinuouslyUpdatesValueBindingOption,
        valueTransformer, NSValueTransformerBindingOption,
        nil];
    [item bind:@"hidden"
      toObject:[[defaultsController arrangedObjects] objectAtIndex:paneNum]
   withKeyPath:@"show"
       options:bindingOptions];

The binding seems to be working to a certain extent; menu items are initially shown or hidden, but the valueTransformer is never used and therefore the "hiddenness" of each menu item is backwards. (As an aside, if the plist changes, the menu items "hiddenness" is also not being updated automatically, as I would expect with bindings.)

What did I miss?

BTW, I realize that it would be easier to just store "hidden" instead of "show" in the plist, but at this point I want to understand why this isn't working.

Edit: My guess is that it has something to do with the fact that I'm trying to bind to an object inside an NSMutableDictionary. If that's the case, what's the right way to do this?


I just ended up using NSNegateBooleanTransformerName (something I found today). I am going to assume there was something stupid in my value transformer or that it has to do with trying to bind to the NSMutableDictionary as a sample project I whipped up that didn't use a dictionary worked fine.

Edit: It was something in my transformer as NSNegateBooleanTransformer is working fine, even with the dictionary in the mix. (A conflict there wouldn't have made sense, anyway).

I'd love for someone to explain why a value transformer based on the code above (should be the same as NSNegateBooleanTransformer) doesn't work, and I'd change my answer acceptance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号