• 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
Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)


  • Subject: Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
  • From: DumberThanDanQuayle <email@hidden>
  • Date: Fri, 24 Sep 2004 22:52:26 -0400

On Sep 24, 2004, at 12:18 PM, Glenn Andreas wrote:

More importantly, though, is that method swizzling is probably not a good strategy to achieve something like multiple inheritance. Not the least problem is that if you are attempt to have one class "look" like another class, it will need to have the same instance vars (instance vars are referenced as offsets from "self", so if you use a method for class Foo and the receiver is actually class Bar, the code thinks that you have Foo's ivar structure when you have Bar's ivar structure - perhaps this was the debugging problem you saw before?)

Glenn, thanks again for your continued assistance.

Great, this now explains the ivar related problem I noticed and basically _rules out_ method swizzling for what I am trying to accomplish here, which is merely writing a method or protocol implementations only once, instead of multiple times (once for each class with a different super-class). Given that these classes needing a common method implementation have different super-class, they often have different ivar structures as well.

I do not care about classes "looking" like other classes here, that is easily accomplished with protocols (or other techniques like "+poseAsClass" or message forwarding as you mentioned). I am merely looking for a way to write implementations once and then use them in ostensibly unrelated classes. This is barely multiple inheritance.

Some kind of C preprocessor macro could work for what I want to do, but I dealing with large blocks of code and I am not sure if a normal "#define" macro is practical with multiple lines. Apparently, I can use multiple line in define by ending each line with a "\" character. Is there someway to use "#define" with delimiting markers?

I suppose using some kind of inline C function would require all ivars to be passed in as well (which would be messy). I mean Obj-C Class ivars do not scope into inline functions, do they?

On the other hand, method forwarding and its friends can achieve a great deal of the functionality that you may be looking for (and provides a way to simulate changing the class heirarchy at runtime - after all, the delegate of an NSWindow is used to make that window behave like something other than the default NSWindow).

I really do not need to virtually modify the class hierarchy at runtime. I am looking at "-forwardInvocation" but I do not see how this could be used to accomplish my goal.


Actually, I think I just found my solution: I am going to try writing my multi-class implementations in separate files and the simply use the C preprocessor "#include" to copy that text into my class as necessary. This is actually a much cleaner solution than swizzling, as I can write even less code. Before I had to write the "+initialize" methods which were actually getting pretty complicated and even dummy methods to feign protocol implementations. Furthermore, my runtime will be unmodified, so I will have less potential problems and I will be totally free to mess it up again at a later date.

Thank you very much for helping me work through this issue. I downloaded your Cocoa debugger, "gandbug," from your site: http://projects.gandreas.com/gandbug/index.html "gandbug" looks very well done and I will try using it soon.

Truly yours, George.

_______________________________________________
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


  • Follow-Ups:
    • Re: Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
      • From: Tor Manders <email@hidden>
    • Re: Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
      • From: Ondra Cada <email@hidden>
References: 
 >Is _objc_flush_caches_(Class) necessary to swizzle methods? (From: DumberThanDanQuayle <email@hidden>)
 >Re: Is _objc_flush_caches_(Class) necessary to swizzle methods? (From: Glenn Andreas <email@hidden>)
 >Re: Is _objc_flush_caches_(Class) necessary to swizzle methods? (From: DumberThanDanQuayle <email@hidden>)
 >Re: Is _objc_flush_caches_(Class) necessary to swizzle methods? (From: Glenn Andreas <email@hidden>)

  • Prev by Date: Re: How to extend Safari -> Context-Menu, Plugin, traps
  • Next by Date: Re: Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
  • Previous by thread: Re: Is _objc_flush_caches_(Class) necessary to swizzle methods?
  • Next by thread: Re: Multiple Inheritence (Was: Is _objc_flush_caches_(Class) necessary to swizzle methods?)
  • Index(es):
    • Date
    • Thread