runtime方法交换

class_getInstanceMethod    得到类的实例方法

class_getClassMethod          得到类的类方法

[objc]view plaincopy

/**

* 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,SELname)

__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,SELname)

__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

测试所用Person类:

[objc]view plaincopy

#import 

@interfacePerson : NSObject

@property(nonatomic, assign) NSInteger age;

@property(nonatomic,copy)NSString* name;

+(Person*)sharedManager;

- (instancetype)init;

+ (void) printDZ;

- (void) printDZL;

@end

#import "Person.h"

@interfacePerson ()

@property(nonatomic,strong)NSString* sex;

@end

@implementationPerson

+(Person*)sharedManager

{

staticPerson*sharedManager;

staticdispatch_once_t onceTest;

dispatch_once(&onceTest, ^{

sharedManager = [[Personalloc]init];

});

NSLog(@"+ method");

returnsharedManager;

}

- (instancetype)init

{

self= [superinit];

if(self) {

self= [superinit];

self.sex=@"-----------";

self.age=19;

self.name=@"sb";

}

returnself;

}

+ (void)printDZ

{

NSLog(@"this is a class method");

}

- (void)printDZL

{

NSLog(@"this is a instance method");

}

@end

测试代码:

[objc]view plaincopy

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

Person* p1= [[Personalloc]init];

Method m1= class_getInstanceMethod([p1class],@selector(printDZL));

Method m2= class_getClassMethod([Personclass],@selector(printDZ));

NSLog(@"测试前:");

[p1printDZL];

[PersonprintDZ];

method_exchangeImplementations(m1, m2);

NSLog(@"测试后:");

[p1printDZL];

[PersonprintDZ];

}

输出:

[objc]view plaincopy

2015-11-0413:37:08.53902-runtime[2776:69899] 测试前:

2015-11-0413:37:08.53902-runtime[2776:69899] this is a instance method

2015-11-0413:37:08.53902-runtime[2776:69899] this is aclassmethod

2015-11-0413:37:08.54002-runtime[2776:69899] 测试后:

2015-11-0413:37:08.54002-runtime[2776:69899] this is aclassmethod

2015-11-0413:37:08.54002-runtime[2776:69899] this is a instance method

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

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

[objc]view plaincopy

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

Person* p1= [[Personalloc]init];

//    Method m1 = class_getInstanceMethod([p1 class], @selector(printDZL));

//    Method m2 = class_getClassMethod([Person class], @selector(printDZ));

Method m1= class_getInstanceMethod([Personclass],@selector(printDZ));

Method m2= class_getClassMethod([p1class],@selector(printDZL));

NSLog(@"测试前:");

[p1printDZL];

[PersonprintDZ];

method_exchangeImplementations(m1, m2);

NSLog(@"测试后:");

[p1printDZL];

[PersonprintDZ];

}

输出:

[objc]view plaincopy

2015-11-0413:38:10.05002-runtime[2793:70426] 测试前:

2015-11-0413:38:10.05102-runtime[2793:70426] this is a instance method

2015-11-0413:38:10.05102-runtime[2793:70426] this is aclassmethod

2015-11-0413:38:10.05102-runtime[2793:70426] 测试后:

2015-11-0413:38:10.05102-runtime[2793:70426] this is a instance method

2015-11-0413:38:10.05102-runtime[2793:70426] this is aclassmethod

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

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

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,856评论 0 9
  • 为 NSString 重写 hasPrefix: 方法实现(交换方法)@implementationNSStrin...
    ganld阅读 511评论 0 0
  • 首先创建两个类 每一个类里面都创建一个方法例如:类1@interface classOne : NSObject...
    俊俊吖阅读 202评论 0 1
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 1,471评论 0 8
  • 写下这个题目,突然想起梁实秋的一篇文章《利用零碎时间》,难道我与大师心有灵犀?兴奋之余想谈谈我对如何利用零碎时间的...
    遇见更美的自己阅读 895评论 1 1