iOS 给父类发送消息

<pre>

@interface NSObject (QMUI)

/**
对 super 发送消息

@param aSelector 要发送的消息
@return 消息执行后的结果
@link http://stackoverflow.com/questions/14635024/using-objc-msgsendsuper-to-invoke-a-class-method
*/

  • (id)performSelectorToSuperclass:(SEL)aSelector;

/**
对 super 发送消息

@param aSelector 要发送的消息
@param object 作为参数传过去
@return 消息执行后的结果
@link http://stackoverflow.com/questions/14635024/using-objc-msgsendsuper-to-invoke-a-class-method
*/

  • (id)performSelectorToSuperclass:(SEL)aSelector withObject:(id)object;

/**
遍历某个 protocol 里的所有方法

@param protocol 要遍历的 protocol,例如 @protocol(xxx)
@param block 遍历过程中调用的 block
*/

  • (void)enumerateProtocolMethods:(Protocol *)protocol usingBlock:(void (^)(SEL selector))block;
    @end
    </pre>

<pre>
@implementation NSObject (QMUI)

  • (id)performSelectorToSuperclass:(SEL)aSelector {
    struct objc_super mySuper;
    mySuper.receiver = self;
    mySuper.super_class = class_getSuperclass(object_getClass(self));

    id (*objc_superAllocTyped)(struct objc_super *, SEL) = (void )&objc_msgSendSuper;
    return (
    objc_superAllocTyped)(&mySuper, aSelector);
    }

  • (id)performSelectorToSuperclass:(SEL)aSelector withObject:(id)object {
    struct objc_super mySuper;
    mySuper.receiver = self;
    mySuper.super_class = class_getSuperclass(object_getClass(self));

    id (*objc_superAllocTyped)(struct objc_super *, SEL, ...) = (void )&objc_msgSendSuper;
    return (
    objc_superAllocTyped)(&mySuper, aSelector, object);
    }

  • (void)enumerateProtocolMethods:(Protocol *)protocol usingBlock:(void (^)(SEL))block {
    unsigned int methodCount = 0;
    struct objc_method_description *methods = protocol_copyMethodDescriptionList(protocol, NO, YES, &methodCount);
    for (int i = 0; i < methodCount; i++) {
    struct objc_method_description methodDescription = methods[i];
    if (block) {
    block(methodDescription.name);
    }
    }
    free(methods);
    }

@end
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容