Re: Class introspection [was Re: Forcing allocation of a subclass]
Re: Class introspection [was Re: Forcing allocation of a subclass]
- Subject: Re: Class introspection [was Re: Forcing allocation of a subclass]
- From: João Varela <email@hidden>
- Date: Sun, 25 Jan 2009 18:54:48 +0000
On 2009/01/25, at 18:02, João Varela wrote:
So you mean that
if ( self == [aReceiver class] )
is the same as
if ( [self class] == [aReceiver class] ) ?
Yes it is.
Just a simple snippet to try to help you to understand.
We are ok to say that in a method, self represents the receiver.
Now have a look at this call:
[PDFDocument allocWithZone:nil]
You are ok to say that it is equivalents to:
[[PDFDocument class] allocWithZone:nil]
With the second form, you clearly see here that the receiver is
[PDFDocument class]. And as self is the receiver, (self ==
[PDFDocument class]) is perfectly valid to test class equality.
Hmm...
I think this example does not cover my doubt expressed above. I
actually now think that both examples I wrote above are wrong or at
least semantically wrong.
The thing is: 'self' is a pointer to class instance and of course an
actual parameter for the Obj-C runtime routines.
The example you gave is that you are talking about class methods
that do not and should not be used with pointers to class instances
but to receivers that are class names (in your example
'PDFDocument') or class objects returned by, using your example,
[PDFDocument class]. As far as I know class objects and class
instances are two different things.
I think the following comparison is semantically wrong:
if ( self == [MyClass class] )
This statement is comparing a pointer to an object instance (self)
to a MyClass object (returned by the invocation of +[NSObject class]
method) and these are two different animals altogether, unless the
Obj-C runtime deals with this and does the right thing.
My fix is actually semantically wrong too:
if ( [self class] == [myClass class] )
because I am using a receiver (self) that should be a class name
(but it is a pointer to a class instance) in a class method, whose
receiver must be a class name or a class object.
Actually the fix should have been:
if ( [self isKindOfClass:[myClass class]] )
because in this case -isKindOfClass is an instance method whose
receiver is a class instance, not a class object.
If my reasoning is not correct then the Obj-C runtime is rather
messy, which I think that is not the case.
JV
OK, to answer myself I just checked and the 'class' method can also be
an instance method of NSObject objects. That explains everything. So
if ( [self class] == [myClass class] )
is as correct as
if ( [self isKindOfClass:[myClass class]] )
because in the first example we are calling -class: in [self class]
and on the right we are calling +class: in [MyClass class]. This
actually surprises me. Even though the +class: method is a NSObject
class method and -class: is part of the NSObject protocol. How is that
possible there is no collision between these two methods. Isn't that a
case of overloading???
JV
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden