Cocoa Grandfather

Is it possible to access the superclass method of superclasses of objects (or grandfathers)?

For instance:

GrandFatherObject : NSObject
SuperObject : GrandFatherObject
SelfObject : SuperObject

From SelfObject:

- (void)overriddenMethod
{
  // For Self
  someCode();

  // For Parent
  [super overriddenMethod];

  // For GrandParent
  ???
}

I only have access to SelfObject (cannot change SuperObject or GrandFatherObject)

+3
source share
3 answers

Yes, you can do it, but it requires a bit more code than just a call super.

More or less, it will be something like this:

#import <objc/runtime.h>

struct objc_super grandsuper;
grandsuper.receiver = self;
grandsuper.class = class_getSuperclass(class_getSuperclass([self class]));

//if _cmd has a non-struct return value:
id grandsuperReturnValue = objc_msgSendSuper(&grandsuper, _cmd, arg1, arg2, ...);
+10
source

Why do you need this? You should rethink your class if you think this is necessary. Of course, you can always call superin the implementation of the parent class overriddenMethod.

, . . .

+5

objc_msgSendSuper(), , , SelfObject, -

@interface SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod;
@end

@implementation SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod {
    return [super overridenMethod];
  }
@end

, SuperObject, , . - ?

+3

Source: https://habr.com/ru/post/1769609/


All Articles