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
- class_getInstanceMethod和class_getClassMethod
- class_getInstanceMethod和class_getClassMethod的区别
- . 和 ->
- || 和&&
- .和::
- “?”和“!”
- #和##
- && 和 ||
- 、和
- #和##
- .//和..//
- 和
- ?和\?
- ./ 和 ../
- #和##
- #、和##
- #和##
- *和&
- hdu 1587
- Java的悲观锁与乐观锁
- IOS AssetsLibrary框架的使用
- hdoj3979Monster【贪心】
- vs_start vs_end详解
- class_getInstanceMethod和class_getClassMethod
- 为什么选择 Java 8 ?
- utf8字符串截取
- lightoj 1078 - Integer Divisibility 【同余】
- 鸟哥的Linux私房菜(基础篇)- 第二十一章、系统配置工具(网络与打印机)与硬件侦测
- 我的杂谈
- extjs 里执行多行选中,和单行选中的操作
- iOS开发笔记--UIView中的坐标转换
- Cousera 502 bad getway 解决方法