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

Re: isa


  • Subject: Re: isa
  • From: Aram Greenman <email@hidden>
  • Date: Sat, 1 Jun 2002 14:22:44 -0700

On Friday, May 31, 2002, at 03:57 PM, John C. Randolph wrote:

On Friday, May 31, 2002, at 02:57 PM, Aram Greenman wrote:

A follow up to my own post:

Previously on this list we were discussing how to get an object to behave like a class one or more steps up in the inheritance hierarchy. I suggested two ways to accomplish this. +instanceMethodForSelector: is one solution, the problem is if the returned IMP sends messages to self those messages will still get sent normally and will invoke the implementation of the object's true class, not the class we want to pose as. This is the same behavior exhibited by messages to super.

I also suggested changing the object's isa temporarily, with the disclaimer that it might be a bad idea.

So my question is, is that really a bad idea? Example:

- (void)foo {

Class wasa = [self class]; // remember true isa

isa = [Foo class]; // pretend to be a Foo

[self bar]; // calls -[Foo bar]

isa = wasa; // reset true isa
}

Obvious pitfalls include that the posing class may not respond to the selector, or that the called method may send self a message the posing class doesn't respond to, or that the called method or any methods it calls in turn may attempt to access instance variables which don't exist in the posing class. The first case can be checked for at runtime, the latter two can't but could only occur if the object was posing only as a class it didn't inherit from.

Assuming that none of the above dangers are present, can anyone see any problem with doing this? I would really like to get some opinions on this.

Well, generally speaking, swizzling your isa pointer is a dangerous thing to do, but one of the benefits of Obj-C being derived from C is that you can do unsafe things when you want.

Do you mean there are specific dangers (beyond what I have identified) or that this is essentially undefined behavior?

Pointer arithmetic, for example, is inherently dangerous, but since the specific dangers are known, precautions can be taken.

The real question here is, what is it you need to do? If your subclass needs to act like something two levels back in its inheritance tree, maybe you subclassed from the wrong parent.

Mostly I am just curious :)

The thread where this originally came up (re. ObjC inits vs. C++ constructors) had to do with sending messages to self in an initalizer, which is _usually_ a bad idea, since it is possible the method called might access unitialized instance variables, depending on where you are in the initializer chain. Setting isa temporarily to the class which defines the -init would protect against this. (Personally I just avoid sending messages to self in an init or at least comment their use, but anyway.)

However, it could potentially be useful in other cases. Dynamic typing and binding allow us to say [fredAstaire dance] and [mcHammer dance] and get two different behaviors. Of course you can get along w/o dynamic typing/binding, but no one on this list, at least, would _want_ to. 'isa-swizzling' could allow us to say, for example, [fredAstaire danceLikeMCHammer]. Of course there are other (complicated) ways to accomplish the same idea, but this seems like a more elegant solution.


Aram
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: isa
      • From: Marcel Weiher <email@hidden>
    • Re: isa
      • From: Ondra Cada <email@hidden>
  • Prev by Date: Re: isa
  • Next by Date: Re: NSMutableAttributedString Formating Problems...
  • Previous by thread: Re: isa
  • Next by thread: Re: isa
  • Index(es):
    • Date
    • Thread