为什么是“self = [[Rectangle alloc] init]在类方法BAD? [英] Why is "self = [[Rectangle alloc] init]" in a class method BAD?

查看:107
本文介绍了为什么是“self = [[Rectangle alloc] init]在类方法BAD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apple的文档Objective-C Programming Language中,第48页说:

  +(Rectangle *)rectangleOfColor :(NSColor *)color 
{
self = [[Rectangle alloc] init]; // BAD
[self setColor:color];
return self;
}

+(id)rectangleOfColor:(NSColor *)color
{
id newInstance = [[Rectangle alloc] init]; // GOOD
[newInstance setColor:color];
return newInstance;
}


+(id)rectangleOfColor:(NSColor *)color
{
id newInstance = [[self alloc] init]; // EXCELLENT
[newInstance setColor:color];
return newInstance;
}

一个是坏,一个是好,另一个是优秀。为什么呢?

解决方案

有第四种模式....



(1)类型不匹配是BAD。



(2)对类yield的静态引用在子类中无法正确运行



(3)动态引用类意味着子类将被实例化为子类实例






(4)

  +(instancetype)rectangleOfColor:(NSColor *)color //Über-bestest evar! 
{
Rectangle * newInstance = [[self alloc] init];
[newInstance setColor:color];
return newInstance;
}

llvm添加了 instancetype 关键字,说哟!这个方法返回任何被调用的类的实例。因此,如果你是上面的子类,你可以:

  RectangleSub * rs = [RectangleSub rectangleOfColor:[NSColor paisleyColor]] ; 

但这会警告(超出可怕的颜色选择):

  RectangleSub * rs = [Rectangle rectangleOfColor:[NSColor puceColor]]; 

在第二种情况下,(id)返回类型不会发出警告。

$ b请注意,我也切换为 Rectangle * 显式地声明 newInstance 。这也更好,因为在该方法的上下文中 newInstance 只能安全地视为 Rectangle *


In the document "Objective-C Programming Language" by Apple, page 48 says:

+ (Rectangle *)rectangleOfColor:(NSColor *) color
{
    self = [[Rectangle alloc] init]; // BAD
    [self setColor:color];
    return self;
}

+ (id)rectangleOfColor:(NSColor *)color
{
     id newInstance = [[Rectangle alloc] init]; // GOOD
     [newInstance setColor:color];
     return newInstance;
}


+ (id)rectangleOfColor:(NSColor *)color
{
     id newInstance = [[self alloc] init]; // EXCELLENT
     [newInstance setColor:color];
     return newInstance;
}

One is bad, one is good, and the other is excellent. Why is that?

解决方案

There is a fourth pattern....

(1) type mismatch is BAD.

(2) static reference to class yields method that won't behave correctly in subclasses

(3) dynamic reference to class means subclasses will be instantiated as subclass instances


(4)

+ (instancetype)rectangleOfColor:(NSColor *)color // Über-bestest evar!
{
     Rectangle *newInstance = [[self alloc] init];
     [newInstance setColor:color];
     return newInstance;
}

llvm added the instancetype keyword that says "yo! this method returns an instance of whatever class it was called on". Thus, if you were to subclass the above, you could:

RectangleSub *rs = [RectangleSub rectangleOfColor:[NSColor paisleyColor]];

But this would warn (beyond the awful color choice):

RectangleSub *rs = [Rectangle rectangleOfColor:[NSColor puceColor]];

Whereas the (id) return type would not warn in the second case.

Note that I also switched declared newInstance to be explicitly of type Rectangle*. This is more better, too, in that within the context of that method, newInstance can only be safely treated as a Rectangle*.

这篇关于为什么是“self = [[Rectangle alloc] init]在类方法BAD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆