After playing around with the the Objective-C mixins code a little, I still hadn’t tested cross-class instance variable access.
I’ll start by mentioning two things I forgot to mention previously; this code is highly experimental, and you shouldn’t attempt to map a method onto another class unless you control the implementation. Unless you control the original method, there’s no way to know what the method is doing, and no guarantee that it won’t change.
The problem appears when accessing instance variables directly; the solution is obviously not to, and to go through accessors, but I still needed to know why it wasn’t working. Consider the following two lines of code:
*(id *)((int8_t *)self + ivar_getOffset(class_getInstanceVariable([ClassA self], "_delegate")))
*(id *)((int8_t *)self + ivar_getOffset(class_getInstanceVariable([self class], "_delegate")))
Both accomplish the same thing but feature a subtle difference; the first uses a constant value for the class – the class the method is being compiled into – the second uses a dynamic one. These are only approximations of what the compiler generates, when I inquired why there’s a discrepancy I was told it’s due to the performance implications. When direct instance variable access is compiled, it takes the form of the first example – for mixin methods if you insist on directly accessing instance variables, you’ll have to write access of the second form yourself.
Photo by itmpa









The most common mistake is forgetting to copy the shared library to your bundle; or specify your executable’s runpaths. Note that even if you forget to copy the library into your build product, your executable will still work when launched under Xcode.
