美文网首页
Runtime-iOS开发中的利器

Runtime-iOS开发中的利器

作者: jackli007 | 来源:发表于2018-11-06 19:26 被阅读0次

导言:最近同事在开发项目过程中,因开发需求,需要代码中调用第三方pod库某个类的对象的私有方法(.m中定义的方法)。
通过多方调研,发现可以通过Runtime实现此种需求。感谢stackoverflow([Steazy])的强大,参考网址(https://stackoverflow.com/questions/14635024/using-objc-msgsendsuper-to-invoke-a-class-method
I made a "normal" Objective-C method for this on a category of NSObject, which will work for both instance and Class objects to allow you to invoke a superclass's implementation of a message externally. Warning: This is only for fun, or unit tests, or swizzled methods, or maybe a really cool game.

代码实现:

@implementation NSObject (Convenience)

-(id)performSelector:(SEL)selector asClass:(Class)class
{
    struct objc_super mySuper = {
        .receiver = self,
        .super_class = class_isMetaClass(object_getClass(self)) //check if we are an instance or Class
                        ? object_getClass(class)                //if we are a Class, we need to send our metaclass (our Class's Class)
                        : class                                 //if we are an instance, we need to send our Class (which we already have)
    };

    id (*objc_superAllocTyped)(struct objc_super *, SEL) = (void *)&objc_msgSendSuper; //cast our pointer so the compiler can sort out the ABI
    return (*objc_superAllocTyped)(&mySuper, selector);
}

接下来:

[self performSelector:@selector(dealloc) asClass:[self superclass]];

等价于:

[super dealloc];

相关文章

  • Runtime-iOS开发中的利器

    导言:最近同事在开发项目过程中,因开发需求,需要代码中调用第三方pod库某个类的对象的私有方法(.m中定义的方法)...

  • iOS运行时Runtime

    相关文档:Runtime-iOS运行时基础篇 相关文档:Runtime-iOS运行时应用篇

  • Stetho利器

     Android开发中的又一件利器——Stetho。  Stetho is a sophisticated deb...

  • Android Http网络开发神兵利器

    Android Http网络开发神兵利器 Android Http网络开发神兵利器 Http协议HTTP简介主要特...

  • Android Studio实战之 ButterKnife 使用

    butterknife作为Android App开发中的一款利器,在Android Studio开发时,不得不使用...

  • Android Studio搭建系统APP开发环境

    前言 在Android的体系中开发普通app使用Android Studio这一利器会非常的方便。但是开发系统ap...

  • Nexus搭建本地Maven仓库

    Nexus搭建本地Maven仓库 1、共享利器,利人利己   在团队协作开发中,每个程序员都有自己的模块开发维护,...

  • How Jbuilder Works

    jbuilder是Rails开发者最常用的gem之一了,自不必多说,它可是 API 开发中的利器,灵活的DSL语法...

  • Java集合源码分析(一)HashMap

    Java中的集合Collection是我们开发中存储数据的利器,它的孩子有List、Map、Set等,根据不同需求...

  • 自定义log

    log在我们开发中的重要性无须我再多言。然而在swift中取消好多开发利器(作者认为),比如宏定义,当然会有更好的...

网友评论

      本文标题:Runtime-iOS开发中的利器

      本文链接:https://www.haomeiwen.com/subject/fifpxqtx.html