![](/img/trans.png)
[英]Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell
[英]Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method
我使用自定义方法初始化自定义视图:
1)在我的视图控制器中,我正在调用自定义视图,并将此数组传递给我的UIView类型的自定义类
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];
customViewObject = [[CustomView alloc] initWithArray:array];
[ParentLayout addSubview:customViewObject];
2)自定义视图。
-(id)initWithArray:(NSArray*)array {
self = [array objectAtIndex:0]; // passing view as self; here it shows leak.
if(self) {}
return self;
}
它给了我一个可能的泄漏,名为“ Returning 'self' while it is not set to the result of '[(super or self) init...]'
可以肯定的是,您不需要:
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];
customViewObject = [array objectAtIndex:0];
在您的代码中,您分配一个视图并松散分配自我。
我有同样的问题,我通过删除这些代码来解决
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];
customViewObject = [array objectAtIndex:0];
从定义init方法开始。
在创建自定义视图的地方而不是在自定义的定义主体中使用上述代码。
编译器抱怨是因为您使用的是init
函数,而没有使用其中一个超级函数。 尽管从逻辑上讲可能是合理的,但从技术上讲,它是对init
函数的滥用,这就是编译器抱怨的原因。 这并不总是一个问题(我有一些代码在修复它之前仅向我发出警告),但是这是一个很好的习惯,不要那样做。 在这种情况下,不能正确使用init函数。 我会写这样的另一个函数:
+(customViewObject *)createWithArray:(NSArray *)array{
customViewObject *returnObject = [array objectAtIndex:0];
return returnObject;
}
但是,查看代码的第一部分,我发现不需要在customViewObject类中具有此类功能。 我建议简单地这样做:
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];
customViewObject = [array objectAtIndex:0];
[ParentLayout addSubview:customViewObject];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.