简体   繁体   English

在我的自定义alloc方法中未将其设置为'[((super或self)init…])的结果时返回'self'

[英]Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method

I init my custom view with my custom method : 我使用自定义方法初始化自定义视图:

1) In My View Controller I am calling custom view and pass this array to my custom class that is of type UIView 1)在我的视图控制器中,我正在调用自定义视图,并将此数组传递给我的UIView类型的自定义类

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];

customViewObject = [[CustomView alloc] initWithArray:array];

[ParentLayout addSubview:customViewObject];

2) Custom View. 2)自定义视图。

 -(id)initWithArray:(NSArray*)array {
      self = [array objectAtIndex:0]; // passing view as self; here it shows leak.
      if(self) {}
      return self;
  }

It giving me possible leak named Returning 'self' while it is not set to the result of '[(super or self) init...]' 它给了我一个可能的泄漏,名为“ Returning 'self' while it is not set to the result of '[(super or self) init...]'

For sure you don't need this, as far as: 可以肯定的是,您不需要:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];

customViewObject = [array objectAtIndex:0];

In your code you alloc a view and loose it assigning self. 在您的代码中,您分配一个视图并松散分配自我。

I have the same problem, i fix it by remove these code 我有同样的问题,我通过删除这些代码来解决

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomViewiPhoneLayout" owner:self options:nil];
customViewObject = [array objectAtIndex:0];

from the definition init method. 从定义init方法开始。

use above code at the place where you create the custom view rather than in custom's definition body. 在创建自定义视图的地方而不是在自定义的定义主体中使用上述代码。

The compiler is complaining because you are using an init function without using one of the super functions. 编译器抱怨是因为您使用的是init函数,而没有使用其中一个超级函数。 Although it may make logical sense, it is technically misuse of the init function, and this is why the compiler is complaining. 尽管从逻辑上讲可能是合理的,但从技术上讲,它是对init函数的滥用,这就是编译器抱怨的原因。 This will not always be a problem (I had some code that only gave me a warning on it before I fixed it), but it is a good practice not to work that way. 这并不总是一个问题(我有一些代码在修复它之前仅向我发出警告),但是这是一个很好的习惯,不要那样做。 In this case, this is not proper use of the init function. 在这种情况下,不能正确使用init函数。 I would write another function like this: 我会写这样的另一个函数:

+(customViewObject *)createWithArray:(NSArray *)array{
     customViewObject *returnObject = [array objectAtIndex:0];
     return returnObject;
}

However, looking at the first bit of code, I see no need to have a function of this sort in the customViewObject class. 但是,查看代码的第一部分,我发现不需要在customViewObject类中具有此类功能。 I would recommend simply doing this: 我建议简单地这样做:

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.

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