如果没有返回值的init **方法会发生什么?

时间:2017-03-07 07:42:32

标签: ios objective-c automatic-ref-counting

在弧命名约定中说:init ***方法的返回值将由调用者保留,但如果我写了一个没有返回值的init ***方法,是否会在ARC中引起一些内存问题?

- (void)initToolbar
{
    // 工具栏
    UIButton *commendEditButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, [Utils getScreenAppSize].width - 15 *2, 32)];
    commendEditButton.backgroundColor = [UIColor whiteColor];
    commendEditButton.layer.borderColor = UIColorFromRGB(0xb6b6b6).CGColor;
    commendEditButton.layer.cornerRadius = 4.f;
    commendEditButton.layer.borderWidth = .5f;
    [commendEditButton addTarget:self action:@selector(showCommentCompose) forControlEvents:UIControlEventTouchUpInside];
    [commendEditButton setImage:[UIImage imageNamed:@"comment-ico-compose"] forState:UIControlStateNormal];
    [commendEditButton setImage:[UIImage imageNamed:@"comment-ico-compose"] forState:UIControlStateHighlighted];
    [commendEditButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [commendEditButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [commendEditButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    [commendEditButton.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    [commendEditButton setTitleColor:UIColorFromRGB(0xc4c4c4) forState:UIControlStateNormal];
    [commendEditButton setTitle:@"发表评论" forState:UIControlStateNormal];
    NSArray *toolbarItems = [NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithCustomView:commendEditButton],nil];
    _toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [Utils getScreenSize].height - UI_TOOL_BAR_HEIGHT - UI_STATUS_BAR_HEIGHT - UI_NAVIGATION_BAR_HEIGHT, [Utils getScreenAppSize].width, UI_TOOL_BAR_HEIGHT)];
    [_toolBar setBackgroundImage:[UIImage imageNamed: @"toolbar_bg.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
    _toolBar.items = toolbarItems;
}

1 个答案:

答案 0 :(得分:0)

如果您没有计算违反许多代码质量规则,那么就不会发生任何不好的事情。

@implementation A

- (void)initTest {
    // do nothing
}

@end


A *instance = [A alloc];
[instance initTest];

请注意,该实例仍然被强烈引用,我们甚至没有调用[super init]

但我们非常幸运,因为init NSObject什么也没做。

在实践中,这非常依赖于超类,理论上这是未定义的行为。当前的实现允许一切正常工作,但确实是可怕的代码。