tags:

views:

17

answers:

1

hi all,

i need to hv an array of 20 objects.

initially array will hv 0 objects. i hv add objbects 1 by 1,till it gets filled with 20 objects. As soon as array gets 20 objects,and i try to insert a new object(for ex 21st object)

it should delete the 20th object and add itself to first position.

i hope i m giving u a clear picture,about wat i am looking for.

hope for a quick reply

regards shishir

+2  A: 

You could create your own class that uses an NSMutableArray for storage. When adding an item, first check whether there are already 20 objects in the array. If there are, remove the last object from the array. Then add the new object at the front.

- (void)addObject:(id)anObject
{
    if ([dataArray count] == 20) {
        [dataArray removeLastObject];
    }
    [dataArray insertObject:anObject atIndex:0];
}

The above will always add the newest object at the front of the array. I guess that is what you want.

Thomas Müller
but how to add array index wise,my array is getting overwrite?i am inserting object like thisappdelegate.completeToArray = [[NSMutableArray alloc] init];[appdelegate.completeToArray addObject:inSender];this always overrights the previous value.regardsshishir
shishir.bobby