Automatic Object Creation

Joined
Dec 6, 2010
Messages
4
Reaction score
0
Points
1
Hi,
I'm very new to this forum stuff.
Obviously I should look for previous forums to check if an answer to my question is present, but since I really don't know what the actual name for this topic I'm having a tough time finding it.

So, here's my question.
I'm trying to create multiple objects from the same class.
Objects can be created with

NSClass *someObject [ NSClass alloc] init];

but what I want to do is to have the program create them for me like this

if (//action -> create)
-find number of objects already created-
NSClass *object1 or *object(numberOfExistingObjects + 1)
[[NSClass alloc] init];

The code above won't do anything, but I just want to give you the idea.

If you know the answer please post it below, and if you know another link, then that'll be great as well.

It would be nice if you can post an example of the code too.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Your question suggests you know little about programming. If so I suggest you buy a good starter book. If not, I still suggest you get an appropriate book for the subject at hand. If you have some experience, download the Stanford iPhone course from iTunes U. Also Apple's documentation is good.

When you post code on the forum, wrap it with
tags. There is a button for that in the advanced editor. Also don't be lazy or sloppy with what you post. It is best to be complete when it comes to code discussion. If you are going to use pseudocode, then it will have to better than what you did here.

As for the problem. I think what you are looking for is a way to add new instances of an object to a mutable array (NSMutableArray).

So you would define your mutable array outside of the method that is using it and since you are likely doing that in another object, define it in the interface file and initialize it in the init method of the implementation file. We'll refer to this phantom array object as myArray below.

So after the above, in the code being called you might have something like this.

Code:
NSObject * myObject = [[NSObject alloc] init];
[myObject setMyString: @"to something of value"]; // if you have a string to setup.
[myArray addObject: myObject];  // increases retain count of myObject.
[myObject release]; // release now if not using further otherwise you leak memory.
[[myArray lastObject] setMyString: @"to something else"]; // Now use myArray to access that last myObject.

I added a couple of lines so you could an idea of work flow.

To get the count in the array would look like this;
Code:
NSUInteger myCount;
myCount = [myArray count];

if (myCount == [myArray count]) then {}  // The comparison will always evaluate to true in this code sequence.
 
OP
Ubereinkunft
Joined
Dec 6, 2010
Messages
4
Reaction score
0
Points
1
Thanks for the tips.

I guess I missed out some details.
I know I can use NSMutableArray. But I'm trying to get quick access to it during the program.

I'll actually explain a good example I want to use it for.

I guess many have played either StarCraft of Command&Conquer.
Well, in that game you can create units with a click of a button.
Basically, what I want to do is to be able to create objects with a click of a button and to be able to use the specific object without the user knowing it.
I thought it would be an easy way to do it this way, but if you do have a better idea, please let me know.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
The information I gave you should be enough for anyone who has learned the basics of Cocoa GUI design and programming. So far, it is sounding to me like you are trying to jump a head of your limited knowledge.

Some of what you have asked are design decisions. Without being involved with what you are doing or at least gaining a good grasp of it, it is difficult to impossible to give constructive ideas. You need to learn Cocoa design patterns. The recommended book for that is titled "Cocoa Design Patterns" by Buck and Yackman. Before you read that, it still sounds to me like you need to gain the basic programming principles as I suggested before.

The basic model of your button example is something like this...

You create a button on a view in IB (Interface Builder). You create an instance variable in your custom class that you want to associate with the button and a method you want to execute when the button is pressed. In IB, you bind the button to those two items. This is such basic stuff that I think I'll leave it to you to do some learning. Explaining it in text is a little long winded and I've written enough for now.

Perhaps start by reading some items at this site: Cocoa Dev Central

Another thing to note, now that Apple has several hardware platforms, it is good practice to say weather you developing for OS X or the iOS devices. Some of the detail in questions and answers vary.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top