繁体   English   中英

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

[英]what would happen for a init** method without return value in arc?

在弧命名约定中说: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;
}

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

@implementation A

- (void)initTest {
    // do nothing
}

@end


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

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

但是我们在这里很幸运,因为在NSObject上进行init没有任何作用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM