0

I was encountering an issue with my code and somebody explained me that it was because I wasn't creating an instance for my property, he told be that I should do that in my init method. What I don't understand is this : I already created my object by doing like this :

   TADIgnoringConstraint *ignorer = [TADIgnoringConstraint new];

I know that I could also use [[TADIgnoringConstraint alloc] init] but I see that the init method is already existing, so should I create a new custom initialization method with another name ? In that case, how do I create a init function ? What does this function returns ?

Community
  • 1
  • 1
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64

4 Answers4

2

[TADIgnoringConstraint new]

Is exactly the same thing with

[[TADIgnoringConstraint alloc] init]

Which allocates the object in memory and initializes it. When you call init on a allocated object, it runs the init method of the object if declared, if not it runs the init of the superclass which returns the object it self.

Now what @Wain told you was to instantiate your array in your init method. Which is the typical practice in most cases.

You can do something like this:

- (id)init
{
    self = [super init];

    if (self) {

        // Set your properties properly here
        // before you actually return the object so they can be ready to use

        _someMutableArray = [NSMutableArray array];

    }

    return self;

} 
Desdenova
  • 5,326
  • 8
  • 37
  • 45
  • 1
    Not exactly. Here's the reference: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1 – Phillip Mills Apr 23 '14 at 12:40
  • @TrevörAnneDenise Not exactly. With `self` syntax you are using the setter/getter of the object while `_` syntax is direct access to the variable. The above method is Apple recommended way of doing it. Apple encourages to use `_` syntax in the `init` method because setter/getter might not be ready while in there. Personally I use `_` in `init`and `self` syntax on everywhere else. – Desdenova Apr 23 '14 at 12:41
1

new is the same as alloc followed by init. In NSObject, new is defined as

+ (id)new
{
    return [[self alloc] init];
}
Michael
  • 6,451
  • 5
  • 31
  • 53
  • I know ! But what I am trying to do is simply to create a custom initialization function, that would create instances of my properties ! – Pop Flamingo Apr 23 '14 at 12:22
0

Looking at the other thread, the assumed problem is that you may not be initializing the tockensArray property. If that's true, then you need to do that in the init method of TADIgnoringConstraint, which you say already exists.

What you need in that case would be something like:

self.tockensArray = [NSMutableArray array];

If that's not the actual problem, show how you declare tockensArray and what is currently in your init.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Actually, even if the init method exists, it wasn't created by me, it just exists by default and that's my problem. Isn't it a little bit "heavy" to override the init method just to create instances ? Or is it something totally normal ? (I'm new to objective-c) – Pop Flamingo Apr 23 '14 at 12:26
  • 1
    Right, the line I quoted would just go into the existing `init` if there is one. Usually you only need custom `init` methods if you are providing optional ways of creating an object. – Phillip Mills Apr 23 '14 at 12:27
0
TADIgnoringConstraint *ignorer = [TADIgnoringConstraint new];

That line of code creates a new object of class TADIgnoringConstraint and stores a pointer to that object into a local variable. Because it is a local variable and apparently not stored anywhere else, it will be deleted by ARC as soon as you leave the function or method where this happens.

I strongly recommend using [[TADIgnoringConstraint alloc] init], mostly for the reason that everybody does. Following conventions means that your code is easier to read, and people including yourself don't need to check whether your problems are caused by not following conventions. (You may say: "But new is the same as alloc + init" to which I would reply "your code isn't working. Would you bet $1000 that it is indeed the same?").

gnasher729
  • 51,477
  • 5
  • 75
  • 98