• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Class vs +class: Cocoa/Obj-C design question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Class vs +class: Cocoa/Obj-C design question


  • Subject: Re: Class vs +class: Cocoa/Obj-C design question
  • From: Bill Bumgarner <email@hidden>
  • Date: Mon, 15 Oct 2007 22:39:55 -0500

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.

Note that while posing is slimy, evil, and largely deprecated, there are any of a number of mechanisms that may now or in the future cause similar behavior within the runtime.

As well, this requirement prevents the symbol referring to a class from getting baked into your binary (kinda like Java's class model). That is, the indirection gives the runtime considerably more flexibility with which to pursue optimizations or provide features.

BTW: +class and +self are synonymous. The metaclass is largely invisible in terms of the ObjC level API used to interrogate the runtime. It can be confusing, too.

b.bum
_______________________________________________

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


  • Follow-Ups:
    • Re: Class vs +class: Cocoa/Obj-C design question
      • From: Chris Suter <email@hidden>
References: 
 >Class vs +class: Cocoa/Obj-C design question (From: Chris Ryland <email@hidden>)
 >Re: Class vs +class: Cocoa/Obj-C design question (From: Bill Bumgarner <email@hidden>)
 >Re: Class vs +class: Cocoa/Obj-C design question (From: Chris Ryland <email@hidden>)

  • Prev by Date: Re: TableView not loading completely
  • Next by Date: Re: Class vs +class: Cocoa/Obj-C design question
  • Previous by thread: Re: Class vs +class: Cocoa/Obj-C design question
  • Next by thread: Re: Class vs +class: Cocoa/Obj-C design question
  • Index(es):
    • Date
    • Thread