gpt4 book ai didi

objective-c - cocoa 爷爷

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:57 25 4
gpt4 key购买 nike

是否可以访问对象父类(super class)(或祖父)的父类(super class)方法?

例如:

GrandFatherObject : NSObject
SuperObject : GrandFatherObject
SelfObject : SuperObject

来自 SelfObject:

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

// For Parent
[super overriddenMethod];

// For GrandParent
???
}

我只能访问 SelfObject(不能修改 SuperObject 或 GrandFatherObject)

最佳答案

是的,你可以做到,但它比仅仅调用 super 需要更多的代码。

或多或少,它会是这样的:

#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, ...);

关于objective-c - cocoa 爷爷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3936727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com