xcode 使用[self class]有什么意义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5667312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:50:02  来源:igfitidea点击:

What's the Point of Using [self class]

objective-cxcodeclassself

提问by user4951

Is this code correct

这段代码正确吗

@implementation Vehicle
+(id) vehicleWithColor:(NSColor*)color {
    id newInstance = [[[self class] alloc] init]; // PERFECT, the class is // dynamically identified
    [newInstance setColor:color];
    return [newInstance autorelease];
}
@end

Why use [self class]

为什么使用[自类]

I thought self already points to the class on static methods (the ones with +)

我认为 self 已经指向静态方法的类(带有 + 的那些)

回答by Sherm Pendley

It's to support subclassing. If you hard-coded the class name, as in [[Vehicle alloc] init], then a subclass of Vehicle would have to override +vehicleWithColor: to make it do the right thing. With [self class], you could create a subclass HarleyDavidson, and [HarleyDavidson vehicleWithColor:[NSColor blackColor]]would do the right thing automatically, creating an instance of HarleyDavidson instead of an instance of Vehicle.

它是为了支持子类化。如果您对类名进行了硬编码,如[[Vehicle alloc] init],则 Vehicle 的子类必须覆盖 +vehicleWithColor: 以使其执行正确的操作。使用[self class],您可以创建一个子类 HarleyDavidson,并且[HarleyDavidson vehicleWithColor:[NSColor blackColor]]会自动做正确的事情,创建 HarleyDavidson 的实例而不是 Vehicle 的实例。

(Edit:)

编辑:)

See Joe's comment below concerning selfvs. [self class]in class methods - In class methods, it doesn't make a difference. But there isa situation where it can. Classes can respond to instance methods that are defined in a root class - -classitself is just such a method, defined as an instance method in the NSObject protocol. So if you extend a root class such as (for example) NSObject by adding an instance method, that method should always use [self class]if it needs to refer to its own Class object.

请参阅下面关于self[self class]类方法中的Joe 的评论- 在类方法中,它没有区别。但是,的情况下就可以了。类可以响应定义在根类中的实例方法——-class本身就是这样一种方法,在 NSObject 协议中定义为实例方法。因此,如果您通过添加实例方法扩展根类(例如)NSObject,则该方法应始终使用,[self class]如果它需要引用其自己的 Class 对象。

回答by Daniel Dickison

You're right: [self class]is unnecessary in a class method (it's more commonly called that in Objective-C rather than "static" method), because selfis already a class, and [self class]returns itself.

你是对的:[self class]在类方法中是不必要的(在 Objective-C 中更常称为它而不是“静态”方法),因为self它已经是一个类,并[self class]返回自身。

But it gets a bit more interesting. In Objective-C, class objects are technically instances of metaclasses. So [self class]in a class method ought to return the metaclass instead of the class itself. But for practical purposes, Objective-C hides the metaclass so it handles this case specially.

但它变得更有趣了。在 Objective-C 中,类对象在技术上是元类的实例。所以[self class]在类方法中应该返回元类而不是类本身。但出于实际目的,Objective-C 隐藏了元类,因此它专门处理这种情况。

Some good reading on this topic:

关于这个主题的一些很好的阅读: