Re: self vs. [self class] in class methods: is there a difference?
Re: self vs. [self class] in class methods: is there a difference?
- Subject: Re: self vs. [self class] in class methods: is there a difference?
- From: email@hidden
- Date: Thu, 16 May 2002 12:27:36 -0700
Rajiv Vijayakumar wrote:
|The latest "Inside Mac OS X: The Objective-C Programming Language"
|suggests that there is a difference between [self alloc] and [[self
|class] alloc] in a class method; I believe this discussion was not
|present in the pre-"Inside Mac OS X" versions of the book. I'm hoping
|someone can clarify this matter for me since my understanding has been
|that in a class method "self" and "[self class]" both point to the same
|class object.
|
|I've reproduced the relevant quote from the book below; it is from page
|100, and the context is a discussion of how to write the convenience
|constructor +[Rectangle rectangleOfColor:]
|
| "...rather than sending the alloc message to self in a class method,
| it's often better to send alloc to [self class]. This way, if the class
| is subclassed, and the rectangleOfColor: message is received by a
| subclass, the instance returned will be the same type as the subclass."
|
|Could someone help me understand the above?
I think whoever wrote that was himself (or herself) confused about it all. Using the code
@interface Thing : NSObject
+ (id) makeThing;
@end
@implementation Thing
+ (id) makeThing
{
NSLog(@"self = %@", NSStringFromClass(self));
return [[self alloc] init];
}
@end
@interface Thingummy : Thing
@end
@implementation Thingummy
@end
executing "[Thing makeThing]" produces "self = Thing"; executing "[Thingummy makeThing]" produces "self = Thingummy". There seems no need to call "[self class]". Even though the method is defined only in class Thing, self clearly refers to the actual class named in the "makeThing" call. (The code I actually used compared self to [self class]; they were equal both times.)
I don't know if you picked up on it while reading, but the original intent of that section of the book was to discourage a completely different practice: assigning to self in a class method, because doing so made it hard to keep track of whether "self" was a class or an object. I suspect the author fell victim to that very confusion, because the suggested style--[[[self class] alloc] init]--makes sense if you're not sure whether self is class or object: either way, "[self class]" returns the class. But if you do the right thing, and leave self alone, the suggestion seems utterly pointless.
(And, yes, you're right: the old version [December 2000, p. 96-97] didn't say to use "[self class]". I just checked.)
|More generally, isn't it true that if p is a pointer to a class object,
|then p == [p class]? (I'm assuming that the class is derived from
|NSObject)
I think it's true, but I don't believe it's ever stated anywhere. But I think it necessarily follows from the definitions of the *class* method "class" (that is, "+ (Class) class"): "returns the class object", which pretty much has to be the same class you're calling "class" for. (For it to return anything else, there's have to be a "meta-class" object, the class of Class, and I've seen it said [though I don't recall where] that there *is* such a meta-class object, but there's no way for a Cocoa program to access it, so "+ class" can't return it.)
(Usefully, p != [p class] if p is *not* a class. This provides a simple test for whether an object is a class or an instance of a class.)
Glen Fisher
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.