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

Re: get superclass instance


  • Subject: Re: get superclass instance
  • From: Thomas Lachand-Robert <email@hidden>
  • Date: Mon, 7 Mar 2005 19:43:25 +0100


Le 7 mars 05, à 19:01, Agent M a écrit :


No it is not a struct it is a KEYWORD. Any call like [super doSomething] is translated by the compiler to a call to objc_msgSendSuper, see
http://developer.apple.com/documentation/Cocoa/Reference/ ObjCRuntimeRef/index.html


Outside of this context, "super" has no meaning. The same thing happens for the name of any class, like NSObject. You can write [NSObject doSomething], but you need to write
[self doSomethingWithClass:[NSObject class]]


In fact, it is a struct, just a very opaque one. Try:
self->class
which compiles fine and returns what [self superclass] returns.

Any id is an objc_object but casting it to objc_class allows you access to instance variables. That's what gdb does:
((struct objc_class*)self)->super_class
is also the same as above.



You are confusing self and super here. "self" is a pointer to the receiver, so self->something or
id x = self;
make sense. "super" is a keyword, so super->something or id x = super doesn't make sense.



-superclass returns the meta-class which is useless in this situation. I need an id which represents the superclass. I have a feeling that the objc-class structure doesn't support such a notion. Oh well.


No this returns the super class, so a call to [invocation setTarget:[myobject superclass]]; will work for your case. Just try it.

I did. It returns a meta-class which describes my superclass. Anything I invoke on it would have to be a class method whereas I need to call an instance method.


Thanks for your ideas. I have a feeling that ids point to instances of classes and superclass "instances" don't get their own ids, so I'm driving down a dead end. NSInvocations simply don't work for this situation.



Ah now I get it. You want to message an object in its superclass implementation, like a call to [super doSomething]. This is not what you told in your first message:


I would like to set the target of an NSInvocation to be the superclass of the current object, however


Such a call would break encapsulation, so it is not permitted by NSInvocation. More generally calls to super are reserved to the implementation of a given class.


However, there are workarounds:
-- if you know the argument sequence, use objc_msgSendSuper;
-- if you know the class of the receiver, implement on it a method (as a category) that calls the required super implementation;
-- as a last resort, you can fake the runtime by changing the "isa" pointer of the object:
// example:
void* oldisa = myobject->isa; // preserve the current isa
myobject->isa = [myobject superclass];
[NSInvocation setTarget:myobject];// the runtime will believe that it is a superclass instance
//call the invocation, etc...
myobject->isa = oldisa; // restore the pointer


This is dirty code though, and it is not guaranteed to work in all cases, or in any future.

Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet à la chose. >> Molière, Tartuffe.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >get superclass instance (From: Agent M <email@hidden>)
 >Re: get superclass instance (From: Thomas Lachand-Robert <email@hidden>)

  • Prev by Date: Re: get superclass instance
  • Next by Date: finder level file copies
  • Previous by thread: Re: get superclass instance
  • Next by thread: Re: get superclass instance
  • Index(es):
    • Date
    • Thread