问题
I hope i did not skip this part in the ObjC manual but is it possible to refer to a class from within one of its class methods? Like in PHP you would use "this" to refer to the current instance, while "self" refers to the instance's class the ObjC equivalent of "this" would be "self", so what would be the ObjC equivalent of PHP's "self", if there is one?
回答1:
Inside a class method, self
refers to the current class (the class's Class object). Inside an instance method, self
refers to the current instance of that class.
回答2:
If self
is an instance of an object, you can get the object's class with [self class]
.
回答3:
Every Objective-C method gets two parameters implicitly: self
, and _cmd
. Inside any method, self
is the receiver of the message that invoked the method, unless you assign a different value to it. In a class method, the receiver is a class. In an instance method, the receiver is an instance.
来源:https://stackoverflow.com/questions/2255237/refering-to-the-class-itself-from-within-a-class-mehod-in-objective-c