   Generic collections in Objective C
  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 arrayNSMutableArray* sprites = [NSMutableArray alloc]; // create sprite arrayif (sprites){[sprites init]; // initialize sprite arraySprite* sprite = [Sprite alloc]; // create new spriteif (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++.   
  Well I just started messing with iPhone programming myself but I do know that Objective C is pretty funky but if you know C++ isn't not that hard to pick.Anyhow, it looks like the debugger gave you a pretty good clue.It sounds like you have create or override NSMutableArray's addObject method for your custom container to work?But no from the little I've read on Objective C there's nothing similar to the STL Vector class in C++ only the containers like NSMutableArray, NSArray,etc that Objective C provides.If I find differently after finishing my Objective C book I'll post it here.;Quote:Original post by EldritchSprite* sprite = [Sprite alloc]; // create new spriteThat code snippet only allocates memory for the sprite - you need to call init to initialise the object:Sprite* sprite = [[Sprite alloc] init];;Quote:Original post by swiftcoderQuote:Original post by EldritchSprite* sprite = [Sprite alloc]; // create new spriteThat code snippet only allocates memory for the sprite - you need to call init to initialise the object:*** Source Snippet Removed ***So I need to call init even on objects of my own classes?;Quote:Original post by EldritchQuote:Original post by swiftcoderQuote:Original post by EldritchSprite* sprite = [Sprite alloc]; // create new spriteThat code snippet only allocates memory for the sprite - you need to call init to initialise the object:*** Source Snippet Removed ***So I need to call init even on objects of my own classes?Looks like it. The examples in my obj-c book I'm reading right now use either the:Sprite* sprite =[ [Sprite alloc] init];orSprite* sprite = [Sprite new];syntax.;  Thanks for this thread.Well I just started messing with iPhone programming myself but I do know that Objective C is pretty funky but if you know C++ isn't not that hard to pick.   ;Quote:Original post by EldritchQuote:Original post by swiftcoderQuote:Original post by EldritchSprite* sprite = [Sprite alloc]; // create new spriteThat code snippet only allocates memory for the sprite - you need to call init to initialise the objectSo I need to call init even on objects of my own classes?Yes. Objective-C separates initialisation from allocation, which enables several very neat features.   ;  also, this line:NSMutableArray* sprites = [NSMutableArray alloc]; // create sprite arrayneeds to become NSMutableArray* sprites = [[NSMutableArray alloc] init]; // create sprite arrayDon't delay it.When you see methods like.... [Sprite new]; the 'new' method is a class method (like a static method in a C++ class). What it's doing internally is pretty much this:+ (Sprite)new {    return [[Sprite alloc] init];}