Calling a method from the init method?

2019-07-04 02:12发布

Say I have two properties defined as such:

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

and a 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;
}
  1. Is this the correct way to set up the headers array in an init method?
  2. Am I allowed to call self.parentTableView from the init method?
  3. Am I allowed to call a method from the init method (in this case, the prepareTags method calls self too. Will self be ready to use, even though the init method hasn't returned yet?

3条回答
看我几分像从前
2楼-- · 2019-07-04 02:45

Respectively (I'd use list formatting but I can't make it work with blockquotes...!):

Is this the correct way to set up the headers array in an init method?

Yes, but there's no point having the array variable, you might as well just do: headersArray = [[NSMutableArray alloc] init];

Am I allowed to call self.parentTableView from the init method?

No, to quote the Apple docs on Practical Memory Management:

Don’t Use Accessor Methods in Initializer Methods and dealloc

. You should access the ivar directly (as you do with headersArray)

Am I allowed to call a method from the init method (in this case, the prepareTags method calls self too. Will self be ready to use, even though the init method hasn't returned yet?

Yes. Just be very careful (your object may not have been fully initialised, it shouldn't use accessor methods so as to comply with the previous restriction, et cetera)

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-04 02:50

The code looks pretty good. Just a couple 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;
}
查看更多
Rolldiameter
4楼-- · 2019-07-04 02:57

There's no need to use a local variable here:

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

Just assign to the instance variable directly:

headersArray = [[NSMutableArray alloc] init];

Am I allowed to call self.parentTableView from the init method?

Yes, although some people might consider that poor design. Consider the fact that properties sometimes have non-obvious complex setter methods that look at other instance variables. Is it wise to do this when your object hasn't been fully initialised?

Am I allowed to call a method from the init method?

Same as above. So long as you aren't relying on anything you haven't initialised yet, it's fine.

查看更多
登录 后发表回答