Hi! I am experiencing some problems with collections in Objective C (doing some programming for the iPhone).
I am trying to use an NSMutableArray object and inserting new objects into it (sprites). Here is my code:
// Initialize mutable array
NSMutableArray* sprites = [NSMutableArray alloc]; // create sprite array
if (sprites)
{
[sprites init]; // initialize sprite array
Sprite* sprite = [Sprite alloc]; // create new sprite
if (sprite)
{
[sprite create]; // setup sprite stuff...
[sprites addObject :sprite]; // add sprite to sprite array, WARNING: this causes an exception...
}
}
The last line, where the new sprite is added to the sprite array fails in the iPhone simulator for some reason. The console spits out the following:
2009-06-22 18:14:09.262 TestProject[166:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray addObject:]: method only defined for abstract class. Define -[__NSPlaceholderArray addObject:]!'
2009-06-22 18:14:09.264 TestProject[166:20b] Stack: (
2444624043,
2471616059,
2444653647,
2444588562,
12717,
11523,
9442,
818017275,
2455677872,
2455676197,
818016218,
2455677872,
2455684669,
2455685817,
2455677872,
2455676197,
818013567,
818022028,
816113908,
816149067,
2455849118,
2444126917,
2444127352,
827745792,
827745989,
816114848,
816160924,
8320
)
(gdb)
Does anybody know how to use NSMutableArray correctly? Is there a better way to store objects dynamically? I am looking for something similar to the STL Vector class in C++.