Re: NSArrayController - error inserting object at arranged object index N
Re: NSArrayController - error inserting object at arranged object index N
- Subject: Re: NSArrayController - error inserting object at arranged object index N
- From: Jonathan Mitchell <email@hidden>
- Date: Thu, 23 Jun 2016 16:32:50 +0100
> On 23 Jun 2016, at 14:27, Jonathan Mitchell <email@hidden> wrote:
>
> The following raises with NSInternalInconsistencyException when :
>
> 1. an NSArrayController filter is in place,
> 2. controller.clearsFilterPredicateOnInsertion = NO
> 3. added content object is rejected by the filter.
>
> Is this behaviour by design?
>
It would seem there is some intention here according to the header:
- (void)insertObject:(id)object atArrangedObjectIndex:(NSUInteger)index; // inserts into the content objects and the arranged objects (as specified by index in the arranged objects) - will raise an exception if the object does not match all filters currently applied
-addObject: obviously calls - insertObject: atArrangedObjectIndex: (the exception stack trace agrees)
The logic of this is a bit puzzling though as setting controller.clearsFilterPredicateOnInsertion == NO seems to designed to fail.
What’s the point of a filter that raises when it filters?
It is possible to catch the exception raised from -addObject: and proceed.
In some cases this may be acceptable but the object that was to be added won’t appear in the NSArrayController content collection which may be an issue if the filterPredicate is subsequently changed.
My workaround was to set controller.clearsFilterPredicateOnInsertion == YES and reset the filterPredicate following -addObject:
It might be possible to subclass NSArrayController but it might turn into a bit of a rabbit hole.
> #import “AppDelegate.h"
>
> @interface AppDelegate ()
>
> @property (weak) IBOutlet NSWindow *window;
> @end
>
> @implementation AppDelegate
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
>
> NSArrayController *controller = [[NSArrayController alloc] initWithContent:@[@"foo"].mutableCopy];
> controller.filterPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
> return [evaluatedObject isKindOfClass:[NSString class]];
> }];
> controller.clearsFilterPredicateOnInsertion = NO;
> [controller addObject:@"bar"];
> [controller addObject:@1]; // raises
> }
> @end
> _______________________________________________
>
> 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
_______________________________________________
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