PDA

View Full Version : creating instances of an object




kevinrichardsuk
Jun 10, 2008, 10:14 AM
Hi,

I think I'm missing a piece of a puzzle if anyone can help that would be great.

I've created a class I need to create a lot of instances of, sometimes 10 instances and maybe 100 at other times.

I've tested this with 2 instances and all works fine..

Icon *myIconA = [[Icon alloc] init];

In my example I used myIconA and myIconB.

How can I get my program to hand out unique object names as and when I need them? It's almost like an array of objects I need but don't know how to do that.

Any help is thoroughly appreciated.

Kevin



officerdick
Jun 10, 2008, 10:19 AM
for (i = 0, i < 10, i++) {


myObject = [[myObject alloc] init]
[myArray insertObject:myObject atIndex:i]
[myObject release]

}

this code will put in 10 unique instances of the object myObject into the array myArray.

In other words they do not need a unique name, just a unique position in the array, you just have to init a new one each time

kevinrichardsuk
Jun 10, 2008, 10:40 AM
officerdick - thanks for the instant response.

So simple a solution too. thanks

kevinrichardsuk
Jun 10, 2008, 10:54 AM
sorry,

what is myArray?

NSArray *myArray;

That doesn't recognise the insertObject method..

officerdick
Jun 10, 2008, 12:01 PM
sorry,

what is myArray?

NSArray *myArray;

That doesn't recognise the insertObject method..

itś an nsmutable array, shuould have called it mymutable array... sorry, not quite sure about the method namr but it is something like it.

kevinrichardsuk
Jun 11, 2008, 04:10 AM
This has helped even more than I thought, I just figured out how to view type definitions and got this working.

Thanks very much.

kevinrichardsuk
Jun 11, 2008, 06:07 AM
Sorry, last question.

I need this array to be accessible from several functions, but myMutableArray is only accessible from the function it was defined in.

I know this must be a basic question, but how can I make it visible to all functions in this file?

Thanks again.

tacoman667
Jun 11, 2008, 06:56 AM
Sorry, last question.

I need this array to be accessible from several functions, but myMutableArray is only accessible from the function it was defined in.

I know this must be a basic question, but how can I make it visible to all functions in this file?

Thanks again.

In the .h interface declaration file you ned to put the NSMutableArray instantiation. Then you can use it anywhere. If you want Objective-C 2.0 to use automatic instantiation then use property declaration.


@interface Foo
{
NSMutableArray *myArray;
}

@property 9nonatomic, retain) NSMutableArray *myArray;


Now in the .m file you need to put:


@implimentation Foo
{
@synthesize myArray;

- (void)init: {
if([self = [super init]) {
//Create array here
}
}


This will make it available throughout the "file" or class.

kevinrichardsuk
Jun 11, 2008, 07:28 AM
Thanks tacoman, I have it working.

tacoman667
Jun 11, 2008, 08:17 AM
Wow, I just realized I still have a ton of typos in there. :)

Good thing I am a programmer, intellisense (Microsoft Visual Studio) and Code Sense (Mac) are my best friends.

kevinrichardsuk
Jun 11, 2008, 10:44 AM
Hmm, its not actually running as I thought.

I've added the following to the header file;


@interface TouchView : UIView {

NSMutableArray *myArray;
}

@property (nonatomic, retain) NSMutableArray *myArray;

- (void)setUpArray;

@end


Now, when I init the array (using the following line in the .m) the simulator just stops running.

myArray = [[NSMutableArray alloc] init];

I can define the NSMutable array locally, directly above the above line and it works fine.

So I'm lead to believe that its to do with my linking between the .h and the .m

Any ideas?

Thanks again - this forum is a godsend.