[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
NSArrayController troubles
On Mar 3, 2004, at 1:08 PM, Robert Cerny wrote:
> a] Exception raised during posting of notification. Ignored.
> exception: *** -[NSCFArray objectAtIndex:]: index (-1) beyond bounds
> (0)
> I created a model, which contains just NSString *name variable.
> Following the samples from Apple and mmalc I made it an object class
> name of NSArrayController. I also added "Add" and "Remove buttons" but
> get an exception them. The exception is raised after clicking the
> first row of table.
If you're using indexed accessors, make sure to check that the index
you're being asked to return is reasonable:
- (id) objectInMyArrayAtIndex: (unsigned int)index
{
id theArray = [self myArray];
if ([theArray count] < 1 || index > [theArray count] - 1) return
nil;
return [theArray objectAtIndex: index];
}
I've encountered situations where the array controller will request
index 2147483647, which I would guess is the unsigned (rollover)
version of -1. I'm guessing this is some sort of bug in the frameworks.
The above will check to make sure the target array isn't empty, and
that the index fits within the array.
> b] I'm not really sure how to combine datasource and binding
I'm not sure you want to combine these two in most cases (mmalc will
correct me if I'm wrong). It seems far more common to combine delegate
and bindings.
> I have some data coming from directory enumerator, which runs in the
> second thread. How can I use it as an source for NSArrayController? I
> checked the docs, and - (void)addObjects:(NSArray *)objects looks like
> a good candidate, but am I right?
What you want to do is add the objects to the array that the
NSArrayController points at. That array effectively becomes the
datasource for the table view. And if necessary (if a changed
notification isn't automatically triggered), send the array controller
a -rearrangeObjects message to reload it.
- Scott
--
Tree House Ideas
http://treehouseideas.com/