KVC with bindings to array elements?

  • Hi everyone; I'm fairly new to KVC.  I have my application working
    fabulously with my data model.  Parameters and sliders and text
    field's values, min, and max are bound to my objective c object.
    Parameter validation works great.

    However now I want to make an static array of values - each element
    bound to a different gui control.

    What key path do I use to specify the n'th NSNumber in a
    NSMutableArray?  I tried the following:

      myData.params.2
      myData.params.@2
      myData.params@2

    And then what is the best way to implement the methods in my object
    to allow for this?

    Or do I have to split them all out of the array and have separate
    methods for each item like:

    - (void) setParams0: (NSNumber *);
      - (void) setParams1 : (NSNumber *);
      - (void) setParams2 : (NSNumber *);
    - (NSNumber *) getParams0;
    - (NSNumber *) getParams1;
    - (NSNumber *) getParams2;

    etc...? I can't imagine I must resort to this..

    All the examples I find for KVC and arrays are assuming that the
    array is something to be filled in by the user with dynamic number of
    items, for table views etc.

    Regards,
    Jeff Koftinoff
  • KVC doesn't support using a keypath to access a specific index in an
    array.

    On Oct 20, 2006, at 9:52 AM, Jeff Koftinoff wrote:

    > What key path do I use to specify the n'th NSNumber in a
    > NSMutableArray?  I tried the following:
    >
    > myData.params.2
    > myData.params.@2
    > myData.params@2
    >
  • You might be better off with an NSDictionary for this. NSArray
    doesn't support what you're trying to do.

    On Oct 20, 2006, at 9:52 AM, Jeff Koftinoff wrote:

    > Hi everyone; I'm fairly new to KVC.  I have my application working
    > fabulously with my data model.  Parameters and sliders and text
    > field's values, min, and max are bound to my objective c object.
    > Parameter validation works great.
    >
    > However now I want to make an static array of values - each element
    > bound to a different gui control.
    >
    > What key path do I use to specify the n'th NSNumber in a
    > NSMutableArray?  I tried the following:
    >
    > myData.params.2
    > myData.params.@2
    > myData.params@2
    >
    > And then what is the best way to implement the methods in my object
    > to allow for this?
    >
    >
    > Or do I have to split them all out of the array and have separate
    > methods for each item like:
    >
    > - (void) setParams0: (NSNumber *);
    > - (void) setParams1 : (NSNumber *);
    > - (void) setParams2 : (NSNumber *);
    > - (NSNumber *) getParams0;
    > - (NSNumber *) getParams1;
    > - (NSNumber *) getParams2;
    >
    > etc...? I can't imagine I must resort to this..
    >
    > All the examples I find for KVC and arrays are assuming that the
    > array is something to be filled in by the user with dynamic number
    > of items, for table views etc.
    >
    > Regards,
    > Jeff Koftinoff
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<hankh...>
    >
    > This email sent to <hankh...>
    >

    Hank Heijink
    www.hankheijink.com
    <hankh...>
  • You can do it yourself by writing your own valueForUndefinedKey:
    which parses something like params@2 itself to return the proper object.

    On Oct 20, 2006, at 8:04 AM, Scott Anguish wrote:

    > KVC doesn't support using a keypath to access a specific index in
    > an array.
    >
    >
    > On Oct 20, 2006, at 9:52 AM, Jeff Koftinoff wrote:
    >
    >> What key path do I use to specify the n'th NSNumber in a
    >> NSMutableArray?  I tried the following:
    >>
    >> myData.params.2
    >> myData.params.@2
    >> myData.params@2
    >>
  • Thank you for your response; But I still have a problem.  I can't
    just have my accessor method return an NSDictionary - Cocoa expects
    it to be an NSArray, and I get an exception saying it can't convert
    the NSDictionary to the NSArray.  I also tried implementing a method
    named:

    valueIn<Key>WithName

    But it does not get called.

    Jeff

    On 20-Oct-06, at 8:09 AM, Hank Heijink wrote:

    > You might be better off with an NSDictionary for this. NSArray
    > doesn't support what you're trying to do.
    >
    > On Oct 20, 2006, at 9:52 AM, Jeff Koftinoff wrote:
    >
    >> Hi everyone; I'm fairly new to KVC.  I have my application working
    >> fabulously with my data model.  Parameters and sliders and text
    >> field's values, min, and max are bound to my objective c object.
    >> Parameter validation works great.
    >>
    >> However now I want to make an static array of values - each
    >> element bound to a different gui control.
    >>
  • Cocoa is right - you can't convert an NSArray to an NSDictionary just
    like that. Do you mean that you absolutely *have* to use an array? If
    so, the dictionary idea isn't going to work.

    What I meant was something like this: in your object, have an
    instance variable like this:

    NSDictionary *someDictionary;

    The stuff that you want to bind your UI controls to would be in
    there, so that

    [someDictionary valueForKey:@"someKey"]

    will give you the value you want to have in the control. Then you
    bind the control to someDictionary.someKey, and that should do it.

    Hank

    On Oct 20, 2006, at 1:20 PM, Jeff Koftinoff wrote:

    > Thank you for your response; But I still have a problem.  I can't
    > just have my accessor method return an NSDictionary - Cocoa expects
    > it to be an NSArray, and I get an exception saying it can't convert
    > the NSDictionary to the NSArray.  I also tried implementing a
    > method named:
    >
    > valueIn<Key>WithName
    >
    > But it does not get called.
    >
    > Jeff
    >
    > On 20-Oct-06, at 8:09 AM, Hank Heijink wrote:
    >
    >> You might be better off with an NSDictionary for this. NSArray
    >> doesn't support what you're trying to do.
    >>
    >> On Oct 20, 2006, at 9:52 AM, Jeff Koftinoff wrote:
    >>
    >>> Hi everyone; I'm fairly new to KVC.  I have my application
    >>> working fabulously with my data model.  Parameters and sliders
    >>> and text field's values, min, and max are bound to my objective c
    >>> object.  Parameter validation works great.
    >>>
    >>> However now I want to make an static array of values - each
    >>> element bound to a different gui control.
    >>>
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<hankh...>
    >
    > This email sent to <hankh...>
    >

    Hank Heijink
    www.hankheijink.com
    <hankh...>
  • On 20-Oct-06, at 10:34 AM, Hank Heijink wrote:

    > Cocoa is right - you can't convert an NSArray to an NSDictionary
    > just like that. Do you mean that you absolutely *have* to use an
    > array? If so, the dictionary idea isn't going to work.
    >

    No, I do not have to use an NSArray... The NSDictionary can work just
    fine for me..

    > What I meant was something like this: in your object, have an
    > instance variable like this:
    >
    > NSDictionary *someDictionary;
    >
    > The stuff that you want to bind your UI controls to would be in
    > there, so that
    >
    > [someDictionary valueForKey:@"someKey"]
    >
    > will give you the value you want to have in the control. Then you
    > bind the control to someDictionary.someKey, and that should do it.
    >

    I did that - Created a dictionary.

    My class has the intance variable:

      NSDictionary *myFader;

    I create it and initialize it with dummy data.  Each item in the
    dictionary is an object with a float 'value'.

    In my object I can do:

        NSLog( @"Value for key 1: %@", [ myFader valueForKeyPath:
    @"1.value" ] );

    And it does print out:

    2006-10-20 11:16:39.301 MacTest[3968] Value for key 1: 1

    But then the nib file loads

    (gdb) continue
    2006-10-20 11:16:39.439 MacTest[3710] An uncaught exception was raised
    2006-10-20 11:16:39.453 MacTest[3710] Cannot create NSArray from
    object {
        0 = < MyFader: 0x37b1c0>;
        1 = < MyFader: 0x37b290>;
        10 = < MyFader: 0x37b370>;
    .... etc..
    } of class NSCFDictionary

    What am I doing wrong?

    Thanks in advance

    jeff

    > Hank
    >
    > On Oct 20, 2006, at 1:20 PM, Jeff Koftinoff wrote:
    >
    >> Thank you for your response; But I still have a problem.  I can't
    >> just have my accessor method return an NSDictionary - Cocoa
    >> expects it to be an NSArray, and I get an exception saying it
    >> can't convert the NSDictionary to the NSArray.  I also tried
    >> implementing a method named:
    >>
    >> valueIn<Key>WithName
    >>
    >> But it does not get called.
    >>
    >> Jeff
    >>
  • I can't tell from this description. Can you expand a bit more? How
    exactly is the binding set up? What type of control, what key path,
    etc.? Can you post the code for your class?

    Hank

    On Oct 20, 2006, at 2:21 PM, Jeff Koftinoff wrote:

    >
    > On 20-Oct-06, at 10:34 AM, Hank Heijink wrote:
    >
    >> Cocoa is right - you can't convert an NSArray to an NSDictionary
    >> just like that. Do you mean that you absolutely *have* to use an
    >> array? If so, the dictionary idea isn't going to work.
    >>
    >
    > No, I do not have to use an NSArray... The NSDictionary can work
    > just fine for me..
    >
    >> What I meant was something like this: in your object, have an
    >> instance variable like this:
    >>
    >> NSDictionary *someDictionary;
    >>
    >> The stuff that you want to bind your UI controls to would be in
    >> there, so that
    >>
    >> [someDictionary valueForKey:@"someKey"]
    >>
    >> will give you the value you want to have in the control. Then you
    >> bind the control to someDictionary.someKey, and that should do it.
    >>
    >
    > I did that - Created a dictionary.
    >
    > My class has the intance variable:
    >
    > NSDictionary *myFader;
    >
    > I create it and initialize it with dummy data.  Each item in the
    > dictionary is an object with a float 'value'.
    >
    > In my object I can do:
    >
    > NSLog( @"Value for key 1: %@", [ myFader valueForKeyPath:
    > @"1.value" ] );
    >
    > And it does print out:
    >
    > 2006-10-20 11:16:39.301 MacTest[3968] Value for key 1: 1
    >
    > But then the nib file loads
    >
    > (gdb) continue
    > 2006-10-20 11:16:39.439 MacTest[3710] An uncaught exception was raised
    > 2006-10-20 11:16:39.453 MacTest[3710] Cannot create NSArray from
    > object {
    > 0 = < MyFader: 0x37b1c0>;
    > 1 = < MyFader: 0x37b290>;
    > 10 = < MyFader: 0x37b370>;
    > .... etc..
    > } of class NSCFDictionary
    >
    > What am I doing wrong?
    >
    > Thanks in advance
    >
    > jeff
    >
    >
    >> Hank
    >>
    >> On Oct 20, 2006, at 1:20 PM, Jeff Koftinoff wrote:
    >>
    >>> Thank you for your response; But I still have a problem.  I can't
    >>> just have my accessor method return an NSDictionary - Cocoa
    >>> expects it to be an NSArray, and I get an exception saying it
    >>> can't convert the NSDictionary to the NSArray.  I also tried
    >>> implementing a method named:
    >>>
    >>> valueIn<Key>WithName
    >>>
    >>> But it does not get called.
    >>>
    >>> Jeff
    >>>
    >
    >

    Hank Heijink
    www.hankheijink.com
    <hankh...>
  • On 20-Oct-06, at 11:28 AM, Hank Heijink wrote:

    > I can't tell from this description. Can you expand a bit more? How
    > exactly is the binding set up? What type of control, what key path,
    > etc.? Can you post the code for your class?
    >
    > Hank
    >

    Thanks for the help - I fixed the problem - I had an invalid binding
    in my nib file. Deleted it and things are much better now

    jeff
previous month october 2006 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