Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: What does [ClassName self]; do?

I'm looking through the source code for the CocoaHTTPServer project, more specifically the HTTPServer.m file and I just don't understand this line:

connectionClass = [HTTPConnection self];

What does this do (is it documented anywhere)? How does it even compile? Should it not be

connectionClass = [HTTPConnection class];
like image 542
jbat100 Avatar asked Feb 22 '12 14:02

jbat100


People also ask

What is self in Objective-C?

self is a special variable in Objective-C, inside an instance method this variable refers to the receiver(object) of the message that invoked the method, while in a class method self will indicate which class is calling.

What does @() mean in Objective-C?

It's Shorthand writing. In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals.


1 Answers

In this context, - (id)self is a method defined on NSObject. It returns the receiver. For a Class it should obviously do the same as a call to the -(Class)class.

Class objects are thus full-fledged objects that can be dynamically typed, receive messages, and inherit methods from other classes. They’re special only in that they’re created by the compiler.

like image 142
Sulthan Avatar answered Nov 05 '22 16:11

Sulthan