Help with Cocoa Bindings

Discussion in 'Mac Programming' started by babysnakes42, Jul 11, 2011.

  1. babysnakes42 macrumors newbie

    Jul 11, 2011
    I am seriously stuck trying to implement bindings in a program I am writing using xCode 4. Here is what my program is doing:

    A sheet attached to a single window appears when the application launches. The sheet contains an NSTableView for selecting services discovered using Bonjour.

    • The sheet's File's Owner is a NSWindowController subclass called "loginSheetController".
    • "loginSheetController" contains an NSMutableArray called "services". This is an array of MSMutableDictionaries. The dictionaries each contain two keys, "service" and "name". "service" is the key for the actual NSNetService object discovered using NSNetServiceBrowser. "name" is the key for the name of the NSNetService.
    • The window contains an NSArrayController named "Array Controller". Its content array is bound to "File's Owner" with a Model Key Path of "services". All boxes underneath are unchecked.
    • The object controller is set to mode "class", class name "NSMutableDictionary", the checkbox for "Prepares Content" is checked, the box "Editable" is checked, and I have added a key called "name".
    • The NSTableViewColumn's Value is bound to "Array Controller", the controller key is "arrangedObjects", and the model key path is "name". All other boxes are unchecked.

    Whenever I attempt to run this program, I get the following error:
    [<NSCFArray 0x10055c480> addObserver:forKeyPath:options:context:] is not supported. Key path: name
    I don't understand why it is giving me this error because I am not asking for a key path of an array, I am asking for the key path of a dictionary. What am I doing wrong? I've read the Apple KVC guide, the Apple Bindings guide, I've searched this forum, cocoa-dev, cocoadevcentral, cocoadev, Cocoa is my Girlfriend, and Stack Overflow, and I still cannot find my answer. I know I must be missing something simple that I just am not seeing.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    That sounds right to me.

    Are you doing any key-value observing in code?
  3. babysnakes42 thread starter macrumors newbie

    Jul 11, 2011
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16)

    I am not doing any Key Value Observing in code.
  4. kainjow Moderator emeritus


    Jun 15, 2000
    I'd suggest reproducing this setup in a new project with dummy data but with everything else being identical. I can only assume the problem is in the code.
  5. babysnakes42 thread starter macrumors newbie

    Jul 11, 2011
    Thank you

    Thank you for the suggestion of making a dummy project. I found the error in my code. I was mislead to believe that I would have to implement the KVC methods in my controller. :)

    However, once I tracked down the error message I originally posted about I figured out another part of my program that was not working. Now I want to know why it wasn't working. :confused:

    Originally, I was using the proxy object [self mutableArrayValueForKey @"services"] and then adding an object to the proxy array. Using the debugger and my dummy project I was able to figure out that this was not working at all. My array was not being updated, and the UI was not being updated.

    Now, I just manually call [self willChangeValueForKey: @"services"] and [self didChangeValueForKey: @"services"] before I add an object to the services array. This works perfectly.

    Why did using the proxy object not work? It seemed like this solution would be the best because I wouldn't need to worry about sending KVC messages at all, and I generally try to use as much abstraction as possible.
  6. kainjow Moderator emeritus


    Jun 15, 2000
    Kind of hard to say without seeing the original code. Could be the array wasn't initialized before you accessed it.

Share This Page