Re: [MyClass class] returning superclass's class
Re: [MyClass class] returning superclass's class
- Subject: Re: [MyClass class] returning superclass's class
- From: Ken Thomases <email@hidden>
- Date: Thu, 20 Nov 2008 07:43:51 -0600
On Nov 20, 2008, at 6:52 AM, Ken Tozier wrote:
On Nov 20, 2008, at 6:46 AM, Alexander Spohr wrote:
Your Mails title says „[MyClass class] returning superclass's
class“ which is not true and Andy just proved that.
I guess if you never instantiate any actual objects then Andy
"proved" it, but try to create an object using one of the
superclass's methods and I think I proved that it doesn't in fact
return expected results.
As Jean-Daniel said, this is a class cluster and a "feature" of the
class cluster implementation, but it is highly counterintuitive.
[MyClass class] should return the class used to instantiate an
object, not some undocumented mystery class somewhere in a class
cluster.
I got it working for my purposes by embedding an NSCharacerSet as a
property of MyClass, so all is good, but [MyClass class] returning
an undocumented class is wrong, in my opinion.
You keep mentioning [MyClass class], except that's not what you're
calling. You're invoking -class (the instance method, rather than the
class method, by the way) on some object. That object is not an
instance of MyClass or, in the case of your actual code, KOptionString.
What you fail to understand is that you never had an instance of
KOptionString. The expression [KOptionString whitespaceCharacterSet]
sends the message whitespaceCharacterSet to the class object
KOptionString (not an instance of KOptionString). Since you didn't
override +whitespaceCharacterSet in your subclass, that message went
to NSCharacterSet, which does what it always does with that message.
It uses whatever internal logic it likes to return some object that
meets its design contract. Notice that the return type of +
[NSCharacterSet whitespaceCharacterSet] is not even NSCharacterSet*,
it's id.
In other words, class methods are just methods. There's no special
status regarding the class object to which you send them. There's no
rule or convention that, because you send a message to MyClass and got
an object back, that that object's class should be MyClass.
Let me ask you this: the return value from (random example) +
[NSPropertyListSerialization
dataFromPropertyList:format:errorDescription:] -- of what class should
that be? When you understand that there's no expectation that it
would be an instance of NSPropertyListSerialization, and you
understand how that's parallel to the behavior of +[KOptionString
whitespaceCharacterSet], then you'll see where you went wrong.
Regards,
Ken
_______________________________________________
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