Calling a method from an init method?

Let's say I have two properties defined as such:

@property (nonatomic, strong) UITableView *parentTableView;
@property (nonatomic, strong) NSMutableArray *headersArray;

and method:

-(void)prepareTags;

and say I have an init method like this:

-(id)initWithParentTableView:(UITableView*)parentTable
{
    if(self = [super init])
    {
        //1
        NSMutableArray *array = [[NSMutableArray alloc] init];
        headersArray = array;
        //2
        self.parentTableView = parentTable;
        //3
        [self prepareTags];
    }
    return self;
}
  • Is this the correct way to set up an array of headers in the init method?
  • Am I allowed to call self.parentTableViewfrom an init method?
  • I am allowed to call a method from the init method (in this case, the prepareTags method calls self). Will it be ready selfto use even if the init method has not yet returned?
+3
source share
3 answers

Accordingly (I would use list formatting, but I can't get it to work with blockquotes ...!):

Is it correct to set up an array of headers in the init method?

, array, : headersArray = [[NSMutableArray alloc] init];

self.parentTableView init?

, Apple :

dealloc

. ivar ( headersArray)

init ( prepareTags self. self, init hasn ' t ?

. ( , , , , ..)

+5

:

NSMutableArray *array = [[NSMutableArray alloc] init];
headersArray = array;

:

headersArray = [[NSMutableArray alloc] init];

self.parentTableView init?

, . , setter, . , ?

init?

, . , , .

+2

The code looks good. Just a couple of notes ...

-(id)initWithParentTableView:(UITableView*)parentTable
{
    // avoid compiler warning about the assignment and the condition in the same statement
    self = [super init];
    if(self)
    {
        //1
        // no need for the extra stack variable
        self.headersArray = [[NSMutableArray alloc] init];

        //2
        // this is all fine from here
        self.parentTableView = parentTable;
        //3
        [self prepareTags];
    }
    return self;
}
0
source

All Articles