Search code examples
objective-cclassclass-methodhttpserver

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];

Solution

  • 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.