Generic collections in Objective C

Started by
6 comments, last by bronxbomber92 14 years, 10 months ago
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++.
Advertisement
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.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by Eldritch
Sprite* sprite = [Sprite alloc]; // create new sprite
That code snippet only allocates memory for the sprite - you need to call init to initialise the object:
Sprite* sprite = [[Sprite alloc] init];

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by Eldritch
Sprite* sprite = [Sprite alloc]; // create new sprite
That 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 Eldritch
Quote:Original post by swiftcoder
Quote:Original post by Eldritch
Sprite* sprite = [Sprite alloc]; // create new sprite
That 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];
or
Sprite* sprite = [Sprite new];
syntax.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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 Eldritch
Quote:Original post by swiftcoder
Quote:Original post by Eldritch
Sprite* sprite = [Sprite alloc]; // create new sprite
That code snippet only allocates memory for the sprite - you need to call init to initialise the object
So 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

also, this line:
NSMutableArray* sprites = [NSMutableArray alloc]; // create sprite array

needs to become
NSMutableArray* sprites = [[NSMutableArray alloc] init]; // create sprite array

Don'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];}

This topic is closed to new replies.

Advertisement