10

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];
Cœur
  • 37,241
  • 25
  • 195
  • 267
jbat100
  • 16,757
  • 4
  • 45
  • 70
  • 1
    Actually it's pretty clear that method `self` called on a class returns the class. The tricky part is why method `class` called on a class returns the class and not its metaclass :) – Sulthan Feb 22 '12 at 20:07
  • 2
    I really wish people would use `[ClassName self]` instead of `[ClassName class]`. That would reduce the confusion between `+class` and `-class`. But alas, it's pretty ingrained at this point – user102008 May 31 '12 at 20:46
  • @Sulthan: and to add to the confusion, in Smalltalk, `ClassName class` *does* return the metaclass ;) – user102008 May 31 '12 at 21:00

4 Answers4

3

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    Thanks for that, I've found an interesting post http://cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html describing classes/meta-classes. Relevant bit: *Every Class in Objective-C is an object itself. This means that the Class structure must start with an isa pointer so that it is binary compatible with the objc_object*. – jbat100 Feb 23 '12 at 10:01
  • That's for the question. I learned a lot when I was trying to answer it. – Sulthan Feb 23 '12 at 10:13
  • that should be `+(Class)class`, not `-(Class)class`, which does something completely different – user102008 May 31 '12 at 20:45
3

[Classname self] is equal to [Classname class] and returns a reference to the class object.

A little sample code illustrates this:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

NSLog(@"Output 1: %@ address:%x",[NSString self], [NSString self]);
NSLog(@"Output 2: %@ address:%x",[NSString class], [NSString class]);

[p release];

}

Output:

2012-02-22 15:36:13.427 Untitled[1218:707] Output 1: NSString address:7b306a08
2012-02-22 15:36:13.428 Untitled[1218:707] Output 2: NSString address:7b306a08
diederikh
  • 25,221
  • 5
  • 36
  • 49
  • I guess I'm confused because the self method declared in the NSObject protocol is an instance method not a class method - (id)self. So is it because the Class is itself an objective c object which can receive a self message? – jbat100 Feb 22 '12 at 14:45
  • Of course it is. When you are declaring a class method, you are actually adding an instance method to the class. Note that you can override a class method in a subclass. See https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html – Sulthan Feb 22 '12 at 15:35
2

[className self]; is same as [className class];
Returns the class object.
For example:

id object = [getSystemEventsAppDelegate self];
id object1 = [getSystemEventsAppDelegate class];  

enter image description here

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
-2

In a very basic nutshell self is a reference to the current object, you pass that as a variable to (in this case) HTTPConnection, then assign the result of that method to the variable.

So if you look at HTTPConnection you'll be able to see how it uses that object reference and what it's going to return.

Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • 1
    What are you talking about? `HTTPConnection` isn't a method, and even if it was, that's not the right syntax for passing an argument in a message. – BoltClock Feb 22 '12 at 14:37