PDA

View Full Version : Objective C Arrays




gizzerd91
Aug 21, 2009, 12:51 AM
I've been out of internet contact for the last week and am returning with a whole slew of questions about arrays. I'll list them all and if you have a way to accomplish any of these, or if there's some documentation that I haven't found yet that I could read and it would answer all my questions, I'd love your help. Here goes:

- How do I add and subtract objects from an array without manually reconstructing an array? (equivalent of pop and push functions in other languages)
- Can you pass arrays as variables in a message (ex: [anObject doSomethingWithArray:array])
- Can you have 2 dimensional arrays, or arrays containing other arrays?

And then another question on an unrelated note:

I want to have a basic class that keeps track of a sprite, that I can instantiate over an over again, which I can keep copies of in an array and use to keep track of all of the sprites moving on my iPod. How should I reference the class file (SpriteInfo.m or SpriteInfo.h) and where should I put it in EAGLView so that I can declare variables of the type SpriteInfo?



TodVader
Aug 21, 2009, 01:19 AM
1 - addObjectAtIndex /*removeOnjectAtIndex methods
2 - You pass it a pointer to the array
3 - Yes

4 - You always call the .h file. You declare objects of type SpriteInfo... unless I'm misunderstanding your question. You can do it like this: SpriteInfo *instanceName = [[SpriteInfo init] alloc];

I suggest that you get Programming in Objective-c 2.0 by Stephan Kochan. I knew lots of languages before comming to Objective-C and it really saved me lots of time getting up and running in making iPhone games.

ghayenga
Aug 21, 2009, 12:12 PM
1 - addObjectAtIndex /*removeOnjectAtIndex methods
2 - You pass it a pointer to the array


You need to use an NSMutableArray instead of an array to add or remove objects.

You pass a reference to the array.