Binding (KVC/KVO) for C struct datatypes

  • I'm trying to create the following programatic binding:

    [view bind:@"frame.size.width" toObject:self withKeyPath:@"width" options:nil];

    Both view and width are instance variables in my controller object declared as:

    NSImageView * view;
    float width;

    In other words, I'm trying to bind the frame width of the NSImageView
    to a float ivar in my object.

    This doesn't seem to work: I get the following runtime error:

    An uncaught exception was raised
    [<NSConcreteValue 0x145a60> valueForUndefinedKey:]: this class is not
    key value coding-compliant for the key size.
    *** Terminating app due to uncaught exception 'NSUnknownKeyException',
    reason: '[<NSConcreteValue 0x145a60> valueForUndefinedKey:]: this
    class is not key value coding-compliant for the key size.'

    It seems the problem here is that [view frame] returns a NSRect
    C-struct which has a size field which itself is a NSSize C-struct,
    which has a float field 'width'. I was hoping that KVC can parse [view
    frame].size.width using the @"frame.size.width" key-path. This doesn't
    seem to be the case.

    How does one implement bindings (or general KVC/KVO) for C-structs ?
  • NSObjects are required. Wrap your struct in an NSValue
    On 04.10.2007, at 15:12, Rudolph van der Merwe wrote:

    > I'm trying to create the following programatic binding:
    >
    > [view bind:@"frame.size.width" toObject:self withKeyPath:@"width"
    > options:nil];
    >
    > Both view and width are instance variables in my controller object
    > declared as:
    >
    > NSImageView * view;
    > float width;
    >
    > In other words, I'm trying to bind the frame width of the NSImageView
    > to a float ivar in my object.
    >
    > This doesn't seem to work: I get the following runtime error:
    >
    > An uncaught exception was raised
    > [<NSConcreteValue 0x145a60> valueForUndefinedKey:]: this class is not
    > key value coding-compliant for the key size.
    > *** Terminating app due to uncaught exception 'NSUnknownKeyException',
    > reason: '[<NSConcreteValue 0x145a60> valueForUndefinedKey:]: this
    > class is not key value coding-compliant for the key size.'
    >
    > It seems the problem here is that [view frame] returns a NSRect
    > C-struct which has a size field which itself is a NSSize C-struct,
    > which has a float field 'width'. I was hoping that KVC can parse [view
    > frame].size.width using the @"frame.size.width" key-path. This doesn't
    > seem to be the case.
    >
    > How does one implement bindings (or general KVC/KVO) for C-structs ?
  • --- Rudolph van der Merwe <rudolphv...> wrote:

    > I'm trying to create the following programatic
    > binding:
    >
    > [view bind:@"frame.size.width" toObject:self
    > withKeyPath:@"width" options:nil];

    There are several problems with this:

    1. KVC is a feature of Cocoa -- only Cocoa classes can
    implement it. A C struct is not a Cocoa class, so you
    can't use KVC to access its members.
    2. You can't bind a keypath even if all the segments
    in it are KVC-compliant. The binding argument has to
    be an exposed binding of the object you're sending the
    message to.
    3.

          ____________________________________________________________________________________
    Check out the hottest 2008 models today at Yahoo! Autos.
    http://autos.yahoo.com/new_cars.html
  • Apologies, Yahoo Mail decided to send my last message
    before I actually wrote the third point. Here's what I
    was trying to say:

    --- Rudolph van der Merwe <rudolphv...> wrote:

    > I'm trying to create the following programatic
    > binding:
    >
    > [view bind:@"frame.size.width" toObject:self
    > withKeyPath:@"width" options:nil];

    There are several problems with this:

    1. KVC is a feature of Cocoa -- only Cocoa classes can
    implement it. A C struct is not a Cocoa class, so you
    can't use KVC to access its members.
    2. You can't bind a keypath even if all the segments
    in it are KVC-compliant. The binding argument has to
    be an exposed binding of the object you're sending the
    message to.
    3. I don't believe "frame" is an exposed binding of
    NSView, so you probably shouldn't try to bind it.

    You could pretty easily create an NSImageView subclass
    that allows this binding, but I would try reading up
    on Bindings a little more first.

    Cheers,
    Chuck

          ____________________________________________________________________________________
    Catch up on fall's hot new shows on Yahoo! TV. Watch previews, get listings, and more!
    http://tv.yahoo.com/collections/3658
  • On Oct 4, 2007, at 11:48 AM, Charles Steinman wrote:

    > 2. You can't bind a keypath even if all the segments
    > in it are KVC-compliant. The binding argument has to
    > be an exposed binding of the object you're sending the
    > message to.

    I don't believe this is true. In fact, mmalc specifically calls this
    out on his bindings page under the "manual bindings" section:

    "The backgroundColor variable is not exposed as an available binding
    in Interface Builder; you can nevertheless establish your own binding
    programatically. There is no need to subclass NSTextField, override
    +exposeBinding".

    <http://homepage.mac.com/mmalc/CocoaExamples/controllers.html>

    That said, not all properties can be bound for all classes, but it's
    not exclusively determined by which bindings are exposed.

        - Scott
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today