creating instances of an object

Discussion in 'Mac Programming' started by kevinrichardsuk, Jun 10, 2008.

  1. macrumors member

    Joined:
    May 19, 2008
    #1
    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
     
  2. macrumors regular

    Joined:
    May 4, 2006
    #2
    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
     
  3. thread starter macrumors member

    Joined:
    May 19, 2008
    #3
    officerdick - thanks for the instant response.

    So simple a solution too. thanks
     
  4. thread starter macrumors member

    Joined:
    May 19, 2008
    #4
    sorry,

    what is myArray?

    NSArray *myArray;

    That doesn't recognise the insertObject method..
     
  5. macrumors regular

    Joined:
    May 4, 2006
    #5
    itś an nsmutable array, shuould have called it mymutable array... sorry, not quite sure about the method namr but it is something like it.
     
  6. thread starter macrumors member

    Joined:
    May 19, 2008
    #6
    This has helped even more than I thought, I just figured out how to view type definitions and got this working.

    Thanks very much.
     
  7. thread starter macrumors member

    Joined:
    May 19, 2008
    #7
    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.
     
  8. macrumors regular

    Joined:
    Mar 27, 2008
    #8
    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.
     
  9. thread starter macrumors member

    Joined:
    May 19, 2008
  10. macrumors regular

    Joined:
    Mar 27, 2008
    #10
    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.
     
  11. thread starter macrumors member

    Joined:
    May 19, 2008
    #11
    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.
     

Share This Page