3

How can i lazily initialize a NSMutableArray of Buttons ? I do something like this :

-(NSMutableArray *)roasteryButtons
 {
     if(!roasteryButtons)
     {
     roasteryButtons = [ NSMutableArray new];
     //other code
     }
return roasteryButtons;
 }

And don't know what to do to call this lazy initializer ? i.e. I need to initialize the array so that i may set the frame for every button in the array

Alex
  • 10,869
  • 28
  • 93
  • 165

4 Answers4

2

I guess you know when to call this method, right ?

The first thing is that you shouldn't use "new" method, but [[NSMutableArray alloc] init] instead : You should have a look at all existing [Init] methods available for NSArray : there are a bunch of them (with capacity, with objects, etc...)

Anyway, you should add some parameters to your method [roasteryButtons] : parameters that will help the method to know, for instance how many buttons to create, what is the frame where they have to show, etc. So this will look a bit like

-(NSMutableArray *)roasteryButtonsWithFrame:(*Frame) andNumbersOfButtons:(int)

for example...

or instead of parameters, you can pass a reference to a delegate that will be able to give answers to those questions (How many buttons, what's my frame and bounds, etc.) So in this case, the method will look like :

-(NSMutableArray *)roasteryButtonsWithDelegate:(id) 

(This delegate should implement a protocol that you will create, containing the different methods that the delegate will have to respond to. ie methods like [howManyButtons]...)

Rabskatran
  • 2,269
  • 5
  • 27
  • 41
  • there's nothing wrong with using `new`, see http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c – mvds Jul 28 '11 at 09:00
  • 1
    No, I know, I've just mentioned that because it can be that it's not really a decision he made, but an habit from another OO language. The variant of the simple Init are normally quite useful. – Rabskatran Jul 28 '11 at 09:03
  • ok. But why is numberofbuttons not just an `int`, or better, `NSInteger`, or even better, `NSUInteger`? `int` is a habit from an ancient language ;-) – mvds Jul 28 '11 at 09:18
2

What u have done is correct. Instead of allocating the array in the init method of class, u are allocating the array only when required. Thus it serves the purpose of lazily allocating.

In the class, Wherever you want the array, you just call,

NSMutableArray *arr = [self roasteryButtons];

Also declare the method in header file as, -(NSMutableArray*)roasteryButtons;.

If you want the reference of the array in other classes, the call like,

[classObj roasteryButtons];

I have shown it as instance method. You can also declare that as class method, if you want like that.

And release that in -(void)dealloc method.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

The Perfect Way to Lazy initialize is as follow

in .h file declare your NSMUtableArray as property as follow

@property (nonatomic,strong) NSMutableArray *array;

Now in .m file synthesize it and do lazy initialize in getter like as follow:

@synthesize array=_array;

(NSMutableArray *) array { (!_array) _array=[[NSMutableArray alloc]init]; //this line is called lazy intialization..this line will create MutableArray at program //run time.

return _array }

Now answer why we need this is that it take care about that if no NSMutableArray is created then it create it at programme run time and like this your app will not crash.

viks
  • 11
  • 1
0

You could make your method a class method:

+(NSMutableArray *)roasteryButtons {

in this way you will be able to call it like this:

[MyRoasteryButtonClass roasteryButtons];

and this will return you your object.

Hope this helps.

sergio
  • 68,819
  • 11
  • 102
  • 123