Re: kvc/kvo for arrays defeated me.
Re: kvc/kvo for arrays defeated me.
- Subject: Re: kvc/kvo for arrays defeated me.
- From: Quincey Morris <email@hidden>
- Date: Fri, 13 Mar 2009 18:00:08 -0700
On Mar 13, 2009, at 15:34, Darren Minifie wrote:
=== Interface file: ===
@interface PerceptionController : NSWindowController {
NSMutableArray* dpcm2;
}
@property(nonatomic, retain) NSMutableArray* dpcm2;
-(void)insertObject:(id)num inDpcm2AtIndex:(unsigned)index;
-(void)removeObjectFromDpcm2AtIndex:(unsigned)index;
-(void)replaceObjectInDpcm2AtIndex:(unsigned)index withObject:(id)num;
=== implementation file ===
@synthesize dpcm2;
-(id)init{
self = [super initWithWindowNibName:@"perceptionPanel"];
self.dpcm2 = [NSMutableArray arrayWithCapacity:6];
for(int i = 0; i<6; i++){
// just adding eight 0's to the array
[dpcm2 addObject:[NSNumber numberWithInt:0]];
}
return self;
}
-(void)insertObject:(id)num inDpcm2AtIndex:(unsigned)index{
[[self dpcm2] insertObject:num atIndex:index];
}
-(void)removeObjectFromDpcm2AtIndex:(unsigned)index{
[[self dpcm2] removeObjectAtIndex:index];
}
-(void)replaceObjectInDpcm2AtIndex:(unsigned)index withObject:
(id)num{
[[self dpcm2] replaceObjectAtIndex:index withObject:num];
}
== end code ==
within interface builder, I have a table column bound to the
arrangedObjects
property of an array controller. The array controller is bound to
the dpcm2
array. Again, the getters are working, but setters arent. One
resource I
found says this:
"For best performance, you should implement the two KVC-compliant
methods
shown in the next listing, instead of the setShapes: method:". (its
in:
Sample KVC-Compliant Accessor Methods apple docs). I dont
understand why
they dont implement the setter for their property, and this may be
the cause
of my problem. Without the setter method how do you ever assign
something
to the property in the first place? The runtime error i receive
when trying
to change a value is:
2009-03-13 15:33:36.142 a4[4494:10b] Error setting value for key
path of
object 0 (from bound object <NSTableColumn: 0x187150>(null)):
[<NSCFNumber
0x1105d0> setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key .
The problem here is that your table column must be bound to a property
of the class of the objects in the array. (That is, you specified
'arrangedObjects' for the controller key path of the column binding,
but you specified nothing for the model key path.)
These objects, according to your code, are of class NSNumber, which
(a) have no properties useful to you here and (b) are immutable
anyway, so can't be edited.
Here's how it's supposed to work:
Typically, an array property like this is best named with a *plural*
noun. So, your methods will be called
insertObject:inMySomethingsAtIndex, removeObjectFromMySomethingsAtIndex.
Then, you create a class called MySomething (singular) that has the
properties you need for your table columns. In this case, maybe you
just have one table column, so the property of MySomething might be
called 'dpcm2' (with values that are NSNumber objects, or of some
scalar numeric type, according to your needs).
Then you bind your NSArrayController to File's Owner, model key path
"MySomethings". You bind your table column to the controller,
controller key path "arrangedObjects", model key path "dpcm2".
When your table column displays a specific row, it retrieves the
MySomething object corresponding to the row (getting it from
arrangedObjects using the rowIndex), asks the object for its dpcm2
property, and displays that value.
When you edit a specific row of your table column, the value you type
in is set as the new value of the corresponding MySomething object. If
your dpcm2 property is a scalar, the typed value (actually a string to
begin with) is automatically converted to a number. If your dpcm2
property is a NSNumber, you must use a numeric formatter on the cell
in the table column to force the conversion to NSNumber, otherwise the
new value will be set as a NSString (which probably isn't what you
want).
The reason it currently works for getting the values is that an empty
model key path is interpreted as "self". Thus each row in the table is
being evaluated as [NSNumber self] which is the number object itself,
which is something the column can display. However, it's never
possible to *change* an object via its self method (and, as I said,
NSNumber objects aren't mutable anyway).
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden