NSObjectController Question

Discussion in 'Mac Programming' started by mdeh, Jan 4, 2010.

  1. mdeh macrumors 6502

    Jan 3, 2009
    Hi all,
    Hope everyone has had a terrific New Year.

    I have asked something similar before, but the fact that I am coming back to this again, tells me that I am missing something. So, let me ask this in a slightly different way.

    Firstly, let me say that the app that demos this works fine.

    A view, with 2 controls ( an NSLabel and an NSSlider).
    A model class ( called MyModel) with a single float property called "myFloat".
    An NSObjectController.

    The ObjectController and MyModel are both represented in the Nib file.

    Both the "value" bindings of NSLabel and NSSlider are bound to the ControllerKey: selection and Model Key Path: myFloat of the NSObjectController, as I would expect.

    Binding of the controller:

    The "Content Object" is bound to the model key path: self, and in IB, after this has been checked, Content Object has in parenthesis after it (MyModel.self).

    (I know that I could have set the content object another way, but I am interested in understanding this better)

    OK...now the question.

    I know that "selection" of an NSObjectController is always the Content Object, so the Control Bindings "sort of" make sense. But, I keep getting flummoxed by the use of "self" in the model key path. ( say vs MyModel.myfloat )

    As part of this query, what method call would IB send to the model to establish this binding? Perhaps that would clarify the use of "self".

    Thanks in advance....and apologies if this revisits an old issue.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    self just represents the object you want to bind to. Don't confuse it with anything else. It's a normal legitimate method (look up self in Xcode's documentation). If you subclass NSObjectController and override bind:toObject:withKeyPath:eek:ptions: and set a breakpoint on it, you'll see what gets passed as the arguments - pretty much what you'd expect.
  3. mdeh thread starter macrumors 6502

    Jan 3, 2009
    Hi Kainjow,
    Firstly, thanks.

    The debugger shows this, in "bind:toObject:withKeyPath: options:"

    {Please see image below}

    Hopefully this is clear. I understand that self is used in the context that all selfs are used, but what I am somewhat bewildered about is this.

    As you can just see from the image, there are 2 selfs. The one is identified as the keyPath (which is expected as this is what was set in IB) and the other presumably ( the first self) refers to ?App, which I am assuming, calls the method? But, what still puzzles me is why this works. I can conceptualize how the value bindings of the controls are set to "selection.myFloatValue" as this represents the content of the controller, in other words, the "model.property". But why would the controller be bound to itself ( if that is indeed what the keyPath is suggesting) ...or perhaps I am not getting what "self" really does in this situation.

    Attached Files:

  4. kainjow Moderator emeritus


    Jun 15, 2000
    It's being bound to self of the object (MyModel), not self of the controller.

    Think of it as controller.content = MyModel.self, but anything.self = anything, so you're setting the content to MyModel directly. Bindings require a key path, so self is what's used when you're binding to the object itself, and not one of its properties.

    I'm not the best at explaining bindings, so HTH.
  5. mdeh thread starter macrumors 6502

    Jan 3, 2009

    Thanks...that does help.

Share This Page