xcode 为什么在目标 c 的静态上下文中允许自我
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6325453/
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
Why is self allowed in static context in objective c
提问by Dani
Why is using self
allowed in static context in Objective-C?
为什么self
在 Objective-C 的静态上下文中允许使用?
I thought it was allowed and then I encountered memory errors that took me a week to find out that self
is not an alias for calling other static methods from the class instead of typing the class name.
我认为这是允许的,然后我遇到了内存错误,我花了一周的时间才发现这self
不是从类中调用其他静态方法而不是键入类名的别名。
Xcode and its compiler seems very smart at finding common pitfalls, why isn't it even generating a warning about something like that?
Xcode 及其编译器在查找常见陷阱方面似乎非常聪明,为什么它甚至不生成关于此类的警告?
回答by Dave DeLong
- There is no such thing as a "static context" in Objective-C. What we have instead are "class methods". They definitely are not"static" methods.
- Class methods (ones prefixed with a
+
) are reallyjust instance methods on a particularClass
object. (did your mind just explode?) And since you have aself
variable accessible in an instance method, you naturally have aself
variable accessible in the class method as well. - In a class method,
self
points to the class itself. - Just as you can do
[self performAction]
inside an instance method to invoke methods on this particular instance, you can do[self performClassAction]
inside a class method to invoke methods on this particular class. - All
Class
objects are subclasses ofNSObject
. So you can use anyNSObject
instance method on anyClass
object. (did your mind just explode again?)
- Objective-C 中没有“静态上下文”这样的东西。相反,我们拥有的是“类方法”。它们绝对不是“静态”方法。
- 类方法(以 a 为前缀的方法
+
)实际上只是特定Class
对象上的实例方法。(你的脑子是不是爆炸了?)而且因为你有一个self
可以在实例方法中访问的变量,你自然也有一个self
可以在类方法中访问的变量。 - 在类方法中,
self
指向类本身。 - 就像您可以
[self performAction]
在实例方法中调用此特定实例上的方法一样,您可以[self performClassAction]
在类方法中调用此特定类上的方法。 - 所有
Class
对象都是 的子类NSObject
。所以你可以NSObject
在任何Class
对象上使用任何实例方法。(你的脑子是不是又爆炸了?)
回答by Jonathan Grynspan
self
is only allowed within the context of an Objective-C method. By "static context" I assume you mean within a class method (that is, one whose signature starts with +
rather than -
.) Your assertion that "self
is not an alias for calling other static methods" is incorrect.
self
仅允许在 Objective-C 方法的上下文中使用。对于“静态上下文”,我假设您的意思是在类方法中(即签名以+
而不是开头的方法-
)。您关于“self
不是调用其他静态方法的别名”的断言是不正确的。
self
is allowed in those cases so that you can:
self
在这些情况下是允许的,以便您可以:
- pass the class around as an object, since all Objective-C classes are themselves objects
- send messages to a class without specifying the class name, in case a method is overridden in a subclass (
[Foo bar]
will useFoo
's implementation always;[self bar]
will use whatever implementation is available inself
.)
- 将类作为对象传递,因为所有 Objective-C 类本身都是对象
- 在不指定类名的情况下向类发送消息,以防在子类中覆盖方法(
[Foo bar]
将Foo
始终使用的实现;[self bar]
将使用self
. 中可用的任何实现)
回答by Sherm Pendley
It's allowed because self
doesrefer to the class object when used in class methods. Is that what you mean by "static context?" If so, what memory errors were you having that suggested otherwise?
这是允许的,因为在类方法中使用时self
确实引用了类对象。这就是“静态上下文”的意思吗?如果是这样,您是否有其他建议的内存错误?