Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kevinrichardsuk

macrumors member
Original poster
May 19, 2008
30
0
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

macrumors regular
May 4, 2006
136
0
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

macrumors member
Original poster
May 19, 2008
30
0
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

macrumors member
Original poster
May 19, 2008
30
0
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

macrumors regular
Mar 27, 2008
143
0
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.

Code:
@interface Foo
{
  NSMutableArray *myArray;
}

@property 9nonatomic, retain) NSMutableArray *myArray;

Now in the .m file you need to put:

Code:
@implimentation Foo
{
@synthesize myArray;

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

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

tacoman667

macrumors regular
Mar 27, 2008
143
0
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

macrumors member
Original poster
May 19, 2008
30
0
Hmm, its not actually running as I thought.

I've added the following to the header file;

Code:
@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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.