objectInXXXAtIndex: exception
objectInXXXAtIndex: exception
- Subject: objectInXXXAtIndex: exception
- From: Bertrand Landry-Hetu <email@hidden>
- Date: Mon, 9 May 2005 15:45:11 -0700
Hi everybody,
I'm having a exception raised in my bindings and I'm not too sure of
the "clean" way to do things.
I have one object Foo that contains an NSMutableArray that I expose
through the name Bar. So Foo has -countOfBar and -objectInBarAtIndex.
Another mechanism than binding can be updating the Bar array.
What happens is that the application starts up, the GUI connects to
the model, everything works. Something triggers an update of our model
and ends up calling removeAllObjects on the NSMutableArray that
represents Bar. So to keep the bindings updated we wrapped that call
in willChangeValueForKey:@"bar", didChangeValueForKey:@"bar", so it
does trigger the NSArrayControllers that is bound to Foo.Bar. And then
the NSArrayControllers tries to select an object that does not exist.
An exception of type NSRangeException occurred. *** -[NSCFArray
objectAtIndex:]: index (0) beyond bounds (0)
1 -[NSCFArray objectAtIndex:] (in Foundation)
2 -[Foo objectInBarAtIndex:] (in CocoaBridge)
3 -[NSArrayController selectedObjects] (in AppKit)
4 -[NSArrayController setContent:] (in AppKit)
5 -[NSArrayDetailBinder _refreshDetailContentInBackground:] (in AppKit)
6 -[NSObject(NSKeyValueObservingPrivate)
_notifyObserversForKeyPath:change:]
7 -[NSController _notifyObserversForKeyPath:change:] (in AppKit)
8 -[NSController observeValueForKeyPath:ofObject:change:context:]
(in AppKit)
9 -[NSObject(NSKeyValueObserverNotification)
didChangeValueForKey:] (in Foundation)
So I could:
1) validate the index passed in to objectInBarAtIndex: and return nil
but that could hide some future bugs
2) remove entries 1 by 1 instead of removeAllObjects. That would suck
since the user would really see a list of many items slowly going
away.
3) ? subclass NSArrayController to fix selectedObjects to validate the
count before getting the objects.
Any suggestions/comments?
Thank you.
Bertrand.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden