I am having trouble getting an core-data backed NSArrayController to work properly in my code. Below is my code:
pageArrayController = [[NSArrayController alloc] initWithContent:nil];
[pageArrayController setManagedObjectContext:[self managedObjectContext]];
[pageArrayController setEntityName:@"Page"];
[pageArrayController setAvoidsEmptySelection:YES];
[pageArrayController setPreservesSelection:YES];
[pageArrayController setSelectsInsertedObjects:YES];
[pageArrayController setClearsFilterPredicateOnInsertion:YES];
[pageArrayController setEditable:YES];
[pageArrayController setAutomaticallyPreparesContent:YES];
[pageArrayController setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]]];
BOOL result = [pageArrayController setSelectionIndex:0];
When I attempt to call setSelectionIndex:, it returns YES, indicating that the selection has been successfully changed. However, any subsequent getSelectionIndex calls to the pageArrayController object returns NSNotFound.
What I don't understand is that if I put the NSArrayController into a NIB, and allow the NIB file to perform the initialization (with all of the same attributes in Interface Builder), the NSArrayController works correctly.
Why is there a difference in behavior? Does the NIB file initialize these types of objects in a special way? Is my initialization of the NSArrayController incorrect?
Any help is appreciated. Thanks.