Obj-C,回归“自我”;而它没有设置为“[(super or self) init...]”的结果?

发布于 2024-12-14 18:32:42 字数 273 浏览 2 评论 0原文

我收到分析器警告,因为升级...

返回'self',而它没有设置为'[(super or self) init...]'的结果

不知道有什么问题它 ?

- (id)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self initLayers];
    }
    return self;
}

I'm getting an analyzer warning, since upgrading...

Returning 'self' while it is not set to the result of '[(super or self) init...]'

Dunno whats wrong with it ?

- (id)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self initLayers];
    }
    return self;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2024-12-21 18:32:55

你返回的自我未初始化

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initLayers];
    }
    return self;
}

your returning self not initialized

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initLayers];
    }
    return self;
}
剑心龙吟 2024-12-21 18:32:53

如果我没记错的话,语法是 self = ... 而不是 self == ...。该语法使用赋值的返回值。

If I recall correctly the syntax is self = ... not self == .... The syntax uses the returned value from assignment.

南烟 2024-12-21 18:32:50

去掉第二个等号。正确的 if 语句是:

if(self = [super initWithFrame:frame])

要点是 super 实现可以返回一个与 self 的当前值不同但仍然有效的对象。在这种情况下,您的 if 语句将为 false,因为对象不同,因此不会发生初始化。但是,由于它返回了一个不同的对象,因此超级实现应该释放旧的自身,这就是您要返回的对象。这意味着您可能返回一个无效的指针。

通过仅使用一个等号,您可以设置变量而不是比较它。由于如果 object 不为 nil,则 if(object) 为 true,因此它等价于:

if((self = [super initWithFrame:frame]) != nil)

或者,更容易理解的版本:

self = [super initWithFrame:frame];
if(self != nil)

此代码将 self 重新分配为超级初始化器返回的值,而不是仅仅假设返回的值是相同的。这与为什么将变量设置为 init... 方法的结果而不是 alloc 很重要的原因相同。

// good
id object = [[MyClass alloc] init];
// bad
id object = [MyClass alloc];
[object init];

Get rid of the second equals sign. The proper if statement is:

if(self = [super initWithFrame:frame])

The point of this is that the super implementation could return a different, but still valid, object than the current value of self. In this case, your if statement will be false, since the objects are different, and so your initialization won't occur. However, since it returned a different object, the super implementation should have released the old self, which is what you are returning. This means you are probably returning an invalid pointer.

By using only one equals sign, you set the variable instead of comparing it. Since if(object) is true if object is not nil, it is equivalent to this:

if((self = [super initWithFrame:frame]) != nil)

Or, the easier to understand version:

self = [super initWithFrame:frame];
if(self != nil)

This code reassigns self to be the value returned by the super initializer, instead of just assuming the value returned is the same. This is the same reason why it is important to set a variable to the result of the init... method and not alloc.

// good
id object = [[MyClass alloc] init];
// bad
id object = [MyClass alloc];
[object init];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文