从init方法调用方法? [英] Calling a method from the init method?

查看:109
本文介绍了从init方法调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个这样定义的属性:

Say I have two properties defined as such:

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

和方法:

-(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. 这是在init方法中设置标头数组的正确方法吗?
  2. 我可以通过init方法调用 self.parentTableView 吗?
  3. 我允许从init方法中调用方法(在这种情况下,prepareTags方法也调用 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?

推荐答案

分别(我将使用列表格式,但不能使其与blockquotes一起使用!!):

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

是的,但是没有必要使用 array 变量,您也可以这样做: headersArray = [[NSMutableArray alloc] init];

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

否,引用

不要在初始化方法中使用访问器方法并取消分配

Don’t Use Accessor Methods in Initializer Methods and dealloc

.您应该直接访问ivar(与 headersArray 一样)

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

是的.请非常小心(您的对象可能尚未完全初始化,不应使用访问器方法来遵守之前的限制,等等)

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)

这篇关于从init方法调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆