class_getInstanceMethod和class_getClassMethod

在开发中有的时候需要获得系统的类,实例方法与类方法,通过自己重写此方法进行IMP指针替换,实现自定义的方法实现替换系统的方法实现。

class_getInstanceMethod得到类的实例方法
class_getClassMethod得到类的类方法
/**  
 * Returns a specified instance method for a given class. 
 *  
 * @param cls The class you want to inspect. 
 * @param name The selector of the method you want to retrieve. 
 *  
 * @return The method that corresponds to the implementation of the selector specified by  
 *  \e name for the class specified by \e cls, or \c NULL if the specified class or its  
 *  superclasses do not contain an instance method with the specified selector. 
 * 
 * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not. 
 */  
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)  
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);  
  
/**  
 * Returns a pointer to the data structure describing a given class method for a given class. 
 *  
 * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve. 
 * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve. 
 *  
 * @return A pointer to the \c Method data structure that corresponds to the implementation of the  
 *  selector specified by aSelector for the class specified by aClass, or NULL if the specified  
 *  class or its superclasses do not contain an instance method with the specified selector. 
 * 
 * @note Note that this function searches superclasses for implementations,  
 *  whereas \c class_copyMethodList does not. 
 */  
OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name)  
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); 

测试:

h.
#import <Foundation/Foundation.h>  
  
@interface Person : NSObject  
// 年龄
@property(nonatomic, assign) NSInteger age;  
// 名字
@property(nonatomic, copy) NSString *name;  
  
+ (Person *)sharedManager;  
  
- (instancetype)init;  
  
+ (void) printDZ;  
  
- (void) printDZL;  
  
@end  
  
  
m.
#import "Person.h"  
  
@interface Person ()  
  
@property(nonatomic, strong) NSString *sex;  
  
@end  
  
@implementation Person  

+ (Person *)sharedManager  
{  
    static Person *sharedManager;  
    static dispatch_once_t onceTest;  
    dispatch_once(&onceTest, ^{  
        sharedManager = [[Person alloc] init];  
    });  
    NSLog(@"+ method");  
    return sharedManager;  
}  

- (instancetype)init  
{  
    self = [super init];  
    if (self) {  
        self = [super init];  
        self.sex = @"男";  
        self.age = 24;  
        self.name = @"橘子";  
    }  
    return self;  
}  
  
+ (void)printDZ  
{  
    NSLog(@"this is a class method");  
}  
  
- (void)printDZL  
{  
    NSLog(@"this is a instance method");  
}  
  
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Person *p1 = [[Person alloc] init];
    Method m1 = class_getInstanceMethod([p1 class], @selector(printDZL));
    Method m2 = class_getClassMethod([Person class], @selector(printDZ));
    NSLog(@"测试前:");
    [p1 printDZL];
    [Person printDZ];
    method_exchangeImplementations(m1, m2);
    NSLog(@"测试后:");
    [p1 printDZL];
    [Person printDZ];
}  

ps:注意引入#import <objc/runtime.h>

输出:

2017-05-08 00:03:49.990 ChangeFont[15717:68399634] 测试前:
2017-05-08 00:03:49.990 ChangeFont[15717:68399634] this is a instance method
2017-05-08 00:03:49.991 ChangeFont[15717:68399634] this is a class method
2017-05-08 00:03:49.991 ChangeFont[15717:68399634] 测试后:
2017-05-08 00:03:49.991 ChangeFont[15717:68399634] this is a class method
2017-05-08 00:03:49.992 ChangeFont[15717:68399634] this is a instance method

由输出可见,方法体交换了,说明class_getInstanceMethod成功得到了实例方法,class_getClassMethod成功得到了类方法

------整理

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,820评论 0 9
  • Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的...
    有一种再见叫青春阅读 3,717评论 0 3
  • 文中的实验代码我放在了这个项目中。 以下内容是我通过整理[这篇博客] (http://yulingtianxia....
    茗涙阅读 4,459评论 0 6
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 5,316评论 0 8
  • 原文出处:南峰子的技术博客 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了...
    _烩面_阅读 5,057评论 1 5