0

I have an NSCollectionView whose content is bound to an NSArrayController's arrangedObjects. When I call addObject: on the array controller, it seems to reallocate the underlying array - I can observe the pointer changing addresses. This isn't acceptable behavior for my particular case, as other objects also depend on the array.

Is this normal behavior or am I doing something wrong? I've seen some alternative solutions, such as directly modifying the array and calling willChangeValueforKey: and didChangeValueForKey: on the controller, but that doesn't seem like the most elegant solution.

I am fairly new to Objective-C and Cocoa, so go easy on me. :)

Thanks!

1 Answers1

0
@interface NSArrayController : NSObjectController {
@private
    void *_reserved4;
    id _rearrangementExtensions;
    NSMutableArray *_temporaryWorkObjects;
    NSUInteger _observedIndexHint;
    NSMutableIndexSet *_selectionIndexes;
    NSMutableArray *_objects;
    NSIndexSet *_cachedSelectedIndexes;
    NSArray *_cachedSelectedObjects;
    NSArray *_arrangedObjects;
}

If you look at NSArrayController's header, the instance variable for arrangedObject is a NSArray, so it will have to create a new array whenever you add a new object and is a normal behaviour.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • Thanks! What's the best way to modify the array directly and still have the view update itself with changes? – Drew Lederman Jan 28 '13 at 02:00
  • Or would I be better off creating my own controller class that works with an NSMutableArray? @TheAmateurProgrammer – Drew Lederman Jan 28 '13 at 23:22
  • @DrewLederman May I ask why you want to use the same array? It seems kind of odd to me. – TheAmateurProgrammer Jan 29 '13 at 01:09
  • Well basically, the array belongs to my "account manager" class, which is a singleton. I was just trying to avoid situations where other objects that might be referencing the accounts would become invalid when the controller modified the array. I just ended up redesigning the account manager so that this wouldn't be an issue. Thanks for your help! – Drew Lederman Jan 29 '13 at 06:05