0

When I use alloc on a class that I created (for example on stack class that has an underlying NSMutableArray object) will it apply alloc to all of the properties of it ? or do I have to override the alloc method to make sure that the alloc is called on properties ?

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

3 Answers3

2

All of the properties won't be alloced for you.
However, don't override alloc for that, override init to alloc and init the properties.

Daniel
  • 30,896
  • 18
  • 85
  • 139
2

You do not need to override the alloc method, the properties will be initialized for you to their default values, that is nil for objects and the default values for the primitive types.

So all you need to do:

[[myObject alloc] init]; or the shortcut [myObject new];

You cannot override the alloc method, all the properties will just be set to their default values. You can however override init, to set initial values to properties (instead of their default values):

- (id) init {
   self = [super init];
     if (self) {
       //initialize some properties
     }
   return self;
}
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • I disagree with one point: Certainly in a MRC environment, you CAN override the alloc method. – James Webster Sep 16 '12 at 20:54
  • Actually I disagree on the fact that properties will alloc init themselves if they are objects. They instead will just be set to nil. – Analog File Sep 16 '12 at 22:00
  • @Oscar, I can think of a reason to: I override `alloc` if I create a singleton to ensure that two instances can't be created – James Webster Sep 16 '12 at 22:06
  • @James you override init for that and do a dyspatch once to allocate your object once. – Oscar Gomez Sep 16 '12 at 22:09
  • 1
    `alloc` actually does initialisation. It initialises the `isa` pointer to the correct class pointer, and initialises all iVars to `0`. – Macmade Sep 16 '12 at 22:10
  • @JamesWebster: I think the more traditional approach is to do that in `init`. So when they do `[Singleton alloc]`, memory is allocated, but then when they call `init` on the result, it releases the memory and returns the existing instance. – Chuck Sep 16 '12 at 22:10
  • @Chuck But then: `Singleton *s = [Singleton sharedInstance]; Singleton *t = [[Singleton alloc] init];` `t` would allocated memory, then fail. If the check is in alloc, it would fail immediately. Have a look at the `alloc` method [here](http://getsetgames.com/2009/08/30/the-objective-c-singleton/) – James Webster Sep 16 '12 at 22:33
  • @AnalogFile MRC is "Manual Reference Counting", the predecessor to ARC (Automatic Reference Counting) – James Webster Sep 16 '12 at 22:33
  • @AnalogFile. For example with Singletons. I override alloc, check if my singleton has already been allocated, and if it hasn't. I return the value of `[super alloc]`, else I return nil. The point isn't to change it's behaviour, just control it. – James Webster Sep 16 '12 at 23:09
  • Yes, I see. And I'll add to your previous reply to @Chuck that some smart ass could try to `alloc` without `init` which is bad, but possible (as alloc actually does zero-initialization). Your singleton prevents it. I like it (wonder if it can be done with ARC too, I'll have to check). – Analog File Sep 16 '12 at 23:18
  • @JamesWebster I'm gonna clean up and delete some comments. Like the MRC question and others that are more a discussion than adding value. If you agree, you could do the same. – Analog File Sep 16 '12 at 23:19
  • -1. As @Macmade points out, `alloc` initializes all instance variables to zero except for `isa`, which `alloc` initializes to point to `self` (the object's class, in this case). – jlehr Sep 18 '12 at 12:29
1

In general, the properties won't be alloced for you. However the ones that you add in IB and set as outlets will automatically be created.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41