Refering to the class itself from within a class mehod in Objective C

泄露秘密 提交于 2019-12-24 02:39:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!