• 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: Determining instance creator
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Determining instance creator


  • Subject: Re: Determining instance creator
  • From: James Bucanek <email@hidden>
  • Date: Fri, 29 Dec 2006 12:21:43 -0700

Michael Watson wrote on Friday, December 29, 2006:
>Let's say I have an instance of AClass, and it creates an instance of
>AnotherClass. During the initialization of AnotherClass, does
>AnotherClass have a way to know what object initialized it?

It depends on what you're asking.

If you want to know what object/class belongs to the code that called the code to create the new object, then no. There's no easy way to determine where any code was called from, and no easy way to correlate that address to a class, and virtually impossible to correlate that to an object.

However, if you are asking if Class C is a subclass of class B, which is subclass of class A, and you are executing -[A init], you can look at isa or [self class] and know if the object is being constructed as an A, B, or C object, and by extension, whether your init code is being called from -[B init] or -[C init].

I've had to use this trick on occasion to create class clusters in a base class that can be subclassed. The -init method looks to see if the client is creating an A object and drops into class cluster mode, where it might delete itself and return some other object. But if the class of the object is B or C, it knows it's just doing the base initialization for a subclass.

--
James Bucanek
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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

References: 
 >Determining instance creator (From: Michael Watson <email@hidden>)

  • Prev by Date: bound textview display glitch
  • Next by Date: what is error -7000 when activating fonts?
  • Previous by thread: Re: Determining instance creator
  • Next by thread: bound textview display glitch
  • Index(es):
    • Date
    • Thread