Problem with NSArrayController and insert

  • I'm trying to learn about bindings by writing some simple test Apps and getting things working. I've hit a problem that I'm not sure how to deal with...

    I have an NSMutableArray called 'testArray' which is a member of my AppController class.
    I have an NSArrayController called 'Array Controller' which is bound to my AppController and has the 'Model Key Path' set to 'testArray'

    At the start of my application I manually init the testArray to have 4 NSString values. This works fine since I have bound an NSPopUpButton to my Array Controller and the entries show up fine.

    I am now trying to insert a new NSString object into my array - I have set the action for an NSButton to my Array Controllers 'insert' method, but when I run my Application and hit the button I get the following output:

    *** -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object

    Which sounds like my Array Controller is dealing with an NSArray instead of an NSMutableArray.... but how can this be since I'm telling it to use my 'testArray' member, which is mutable ?

    What bindings do I need to set up so that my 'Insert' button inserts a new NSString object into my array ?

    Sorry if this is trivial - my head is busting from reading docs and I'm looking for a quick nudge in the right direction. The move the IB3.0 is also throwing me a little.

    Thank you.
  • You're right - you are dealing with an NSArray instead of its mutable
    counterpart. Your array should be bound to 'Content Array' in the
    array controller, I think. Your terminology is a bit confusing - you
    say your array controller is bound to your AppController. Do you mean
    your AppController has an IBOutlet that connects to your Array
    Controller?

    Could you post the code that changes your testArray? Most likely,
    you're calling a method that returns an NSArray instead of an
    NSMutableArray and assigning that to your testArray.

    Hank

    On Jan 4, 2008, at 11:34 AM, Martin Linklater wrote:

    > I'm trying to learn about bindings by writing some simple test Apps
    > and getting things working. I've hit a problem that I'm not sure how
    > to deal with...
    >
    > I have an NSMutableArray called 'testArray' which is a member of my
    > AppController class.
    > I have an NSArrayController called 'Array Controller' which is bound
    > to my AppController and has the 'Model Key Path' set to 'testArray'
    >
    > At the start of my application I manually init the testArray to have
    > 4 NSString values. This works fine since I have bound an
    > NSPopUpButton to my Array Controller and the entries show up fine.
    >
    > I am now trying to insert a new NSString object into my array - I
    > have set the action for an NSButton to my Array Controllers 'insert'
    > method, but when I run my Application and hit the button I get the
    > following output:
    >
    > *** -[NSCFArray insertObject:atIndex:]: mutating method sent to
    > immutable object
    >
    > Which sounds like my Array Controller is dealing with an NSArray
    > instead of an NSMutableArray.... but how can this be since I'm
    > telling it to use my 'testArray' member, which is mutable ?
    >
    > What bindings do I need to set up so that my 'Insert' button inserts
    > a new NSString object into my array ?
    >
    > Sorry if this is trivial - my head is busting from reading docs and
    > I'm looking for a quick nudge in the right direction. The move the
    > IB3.0 is also throwing me a little.
    >
    > Thank you.
    >

    Hank Heijink
    <hank.list...>
  • Gah - I am an idiot. Yes, you are correct... I was alloc'ing an NSArray. I was thinking the problem was with my understanding of Bindings rather than my own stupidity. I am now able to insert a new object...

    Thanks.

    On Friday, January 04, 2008, at 04:47PM, "Michael Babin" <mbabin...> wrote:
    > Not doing something trivially wrong like the following, are you?
    >
    > NSMutableArray    *testArray = [[NSArray alloc] init];
    >
    > On Jan 4, 2008, at 10:34 AM, Martin Linklater wrote:
    >
    >> I'm trying to learn about bindings by writing some simple test Apps
    >> and getting things working. I've hit a problem that I'm not sure how
    >> to deal with...
    >>
    >> I have an NSMutableArray called 'testArray' which is a member of my
    >> AppController class.
    >> I have an NSArrayController called 'Array Controller' which is bound
    >> to my AppController and has the 'Model Key Path' set to 'testArray'
    >>
    >> At the start of my application I manually init the testArray to have
    >> 4 NSString values. This works fine since I have bound an
    >> NSPopUpButton to my Array Controller and the entries show up fine.
    >>
    >> I am now trying to insert a new NSString object into my array - I
    >> have set the action for an NSButton to my Array Controllers 'insert'
    >> method, but when I run my Application and hit the button I get the
    >> following output:
    >>
    >> *** -[NSCFArray insertObject:atIndex:]: mutating method sent to
    >> immutable object
    >>
    >> Which sounds like my Array Controller is dealing with an NSArray
    >> instead of an NSMutableArray.... but how can this be since I'm
    >> telling it to use my 'testArray' member, which is mutable ?
    >>
    >> What bindings do I need to set up so that my 'Insert' button inserts
    >> a new NSString object into my array ?
    >>
    >> Sorry if this is trivial - my head is busting from reading docs and
    >> I'm looking for a quick nudge in the right direction. The move the
    >> IB3.0 is also throwing me a little.
    >>
    >> Thank you.
    >>
    >
    >
    >
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today