Sign in to follow this  

Generic collections in Objective C

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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];
}

Share this post


Link to post
Share on other sites

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this