Re: Class vs +class: Cocoa/Obj-C design question
Re: Class vs +class: Cocoa/Obj-C design question
- Subject: Re: Class vs +class: Cocoa/Obj-C design question
- From: Chris Suter <email@hidden>
- Date: Tue, 16 Oct 2007 14:23:00 +1000
On 16/10/2007, at 1:39 PM, Bill Bumgarner wrote:
On Oct 15, 2007, at 9:47 PM, Chris Ryland wrote:
I appreciate that *instance* classes may change dynamically (e.g.,
NSArray *array = [[NSArray alloc] init] may end up with array not
being an actual NSArray instance but some subclass in the
cluster), but are there real-life cases where a class itself
(e.g., literally NSObject) can change its class? Where the literal
expression [NSObject class] wouldn't be NSObject itself?
I would think the more appropriate way to work around any language
oddities with type names, etc. would be [NSObject self] to use a
message to get at the class itself, rather than [NSObject class],
which should, in theory, return the class of NSObject, i.e., its
metaclass.
Forgive me for pushing back (and I know you would know this, if
anyone in the world would), but I'm still wondering (and perhaps
still confused).
No worries -- There is a difference between claiming knowledge and
showing code.
Given that I'm currently in the "Show Me" state, here ya go!
Given this:
@interface Foo: NSObject
@end
@implementation Foo
@end
int main (int argc, const char * argv[]) {
NSLog(@"Pre Pose NSObject--- %@: 0x%x", [NSObject class],
[NSObject class]);
NSLog(@"Pre Pose Foo-------- %@: 0x%x", [Foo class], [Foo class]);
[Foo poseAsClass: [NSObject class]];
NSLog(@"Post Pose NSObject-- %@: 0x%x", [NSObject class],
[NSObject class]);
return 0;
}
It'll spew this (edited for brevity):
Pre Pose NSObject--- NSObject: 0xa07eccc0
Pre Pose Foo-------- Foo: 0x3020
Post Pose NSObject-- NSObject: 0x3020
So, while "NSObject" still refers to the class "NSObject",
posing effectively shoves "Foo" into the runtime in place of
"NSObject". It is sort of like the isa swizzling that occurs
during KVO setup, but at the class level.
But, after posing, there's still no difference between using NSObject
and [NSObject class] so it doesn't prove that there are cases where
NSObject != [NSObject class]
Can you think of another example that does?
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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