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

4

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];
Egeria answered 22/2, 2012 at 14:31 Comment(3)
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 :)Booma
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 pointDissentious
@Sulthan: and to add to the confusion, in Smalltalk, ClassName class does return the metaclass ;)Dissentious
B
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.

Booma answered 22/2, 2012 at 14:36 Comment(3)
Thanks for that, I've found an interesting post cocoawithlove.com/2010/01/… 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.Egeria
That's for the question. I learned a lot when I was trying to answer it.Booma
that should be +(Class)class, not -(Class)class, which does something completely differentDissentious
E
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
Edmon answered 22/2, 2012 at 14:39 Comment(2)
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?Egeria
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 developer.apple.com/library/ios/#documentation/cocoa/conceptual/…Booma
C
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

Cap answered 22/2, 2012 at 14:47 Comment(0)
L
-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.

Lapwing answered 22/2, 2012 at 14:35 Comment(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.Becht

© 2022 - 2024 — McMap. All rights reserved.