class_getInstanceMethod和class_getClassMethod

来源:互联网 发布:linux管道 文件 编辑:程序博客网 时间:2024/06/07 08:50

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);

测试所用Person类:

#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#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 = 19;        self.name = @"sb";    }    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];     }

输出:

2015-11-04 13:37:08.539 02-runtime[2776:69899] 测试前:2015-11-04 13:37:08.539 02-runtime[2776:69899] this is a instance method2015-11-04 13:37:08.539 02-runtime[2776:69899] this is a class method2015-11-04 13:37:08.540 02-runtime[2776:69899] 测试后:2015-11-04 13:37:08.540 02-runtime[2776:69899] this is a class method2015-11-04 13:37:08.540 02-runtime[2776:69899] this is a instance method

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

但是当用class_getInstanceMethod来取类方法,用class_getClassMethod来取实例方法时:


- (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));    Method m1 = class_getInstanceMethod([Person class], @selector(printDZ));    Method m2 = class_getClassMethod([p1 class], @selector(printDZL));    NSLog(@"测试前:");    [p1 printDZL];    [Person printDZ];    method_exchangeImplementations(m1, m2);    NSLog(@"测试后:");    [p1 printDZL];    [Person printDZ];}
输出:

2015-11-04 13:38:10.050 02-runtime[2793:70426] 测试前:2015-11-04 13:38:10.051 02-runtime[2793:70426] this is a instance method2015-11-04 13:38:10.051 02-runtime[2793:70426] this is a class method2015-11-04 13:38:10.051 02-runtime[2793:70426] 测试后:2015-11-04 13:38:10.051 02-runtime[2793:70426] this is a instance method2015-11-04 13:38:10.051 02-runtime[2793:70426] this is a class method

由输出可见,没有发生交换,可知这样是取不到的。


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 工商证没年检怎么办 工行信用卡被锁怎么办 外地卡密码锁了怎么办 营业执照年审过期了怎么办 工商营业执照吊销了怎么办 小规模企业工商年检怎么办 血流变检查偏高怎么办 信誉卡没有邮箱怎么办 税务年报没报怎么办 工商忘记年审了怎么办 营业执照脱审了怎么办 公司年审没有弄怎么办 车检标志丢了怎么办 机动车年检丢了怎么办 汽车保险标志丢了怎么办 车辆年检贴丢失怎么办 车检标贴丢了怎么办 检验标贴丢了怎么办 小车换年检标志怎么办 职称证忘记审验怎么办 联合年报没报怎么办 职称年审过期了怎么办 嘉兴驾模预约怎么办 电大考试去不了怎么办 负债高怎么办房贷 宁波鄞州银行卡怎么办 硕士学位实验数据不好怎么办 小区外过车很吵怎么办 建筑物初级消防证书怎么办 毕业答辩去不了怎么办 卡波西水痘样疹怎么办 58企业认证失败怎么办 炸鸡店没生意怎么办 你好漂亮重名了怎么办 医保卡被冒用怎么办 理财钱被骗走怎么办 个体户怎么办三方协议 社保户籍错了怎么办 身份证号码变更后驾驶证怎么办 公司倒闭欠工资怎么办 海南买房要社保怎么办?