class_getInstanceMethod和class_getClassMethod的区别

https://blog.csdn.net/baidu_25743639/article/details/51793764

今天在学习runtime的method_exchangeImplementations进行方法交换的时候,将class_getInstanceMethod方法误写成class_getClassMethod,结果发现方法交换失败,

找了好久发现在获取方法的时候写错了;

先简单介绍下这两个方法是干什么用的:

 

class_getInstanceMethod      得到类的实例方法

 

class_getClassMethod          得到类的类方法 

从官方文档中我们可以看到:

 

 

/**

* Returns a specified instance method for a given class.
  1. *
  2. * @param cls The class you want to inspect.
  3. * @param name The selector of the method you want to retrieve.
  4. *
  5. * @return The method that corresponds to the implementation of the selector specified by
  6. * \e name for the class specified by \e cls, or \c NULL if the specified class or its
  7. * superclasses do not contain an instance method with the specified selector.
  8. *
  9. * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
  10. */
  11. OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name)
  12. __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
  13. /**
  14. * Returns a pointer to the data structure describing a given class method for a given class.
  15. *
  16. * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
  17. * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
  18. *
  19. * @return A pointer to the \c Method data structure that corresponds to the implementation of the
  20. * selector specified by aSelector for the class specified by aClass, or NULL if the specified
  21. * class or its superclasses do not contain an instance method with the specified selector.
  22. *
  23. * @note Note that this function searches superclasses for implementations,
  24. * whereas \c class_copyMethodList does not.
  25. */
  26. OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name)
  27. __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

好了废话不多说,上代码来说明一下: 

 

DEMO:

测试所用的Person类:

 

 

#import <Foundation/Foundation.h>

    1. @interface Person : NSObject
    2. @property(nonatomic, assign) NSInteger age;
    3. @property(nonatomic, copy) NSString * name;
    4. +(Person *)sharedManager;
    5. - (instancetype)init;
    6. + (void) printDZ;
    7. - (void) printDZL;
    8. @end
    9. #import "Person.h"
    10. @interface Person ()
    11. @property(nonatomic, strong) NSString * sex;
    12. @end
    13. @implementation Person
    14. +(Person *)sharedManager
    15. {
    16. static Person *sharedManager;
    17. static dispatch_once_t onceTest;
    18. dispatch_once(&onceTest, ^{
    19.         sharedManager = [[Person alloc] init];
    20.     });
    21. NSLog(@"+ method");
    22. return sharedManager;
    23. }
    24. - (instancetype)init
    25. {
    26. self = [super init];
    27. if (self) {
    28. self = [super init];
    29. self.sex = @"-----------";
    30. self.age = 19;
    31. self.name = @"sb";
    32.     }
    33. return self;
    34. }
    35. + (void)printDZ
    36. {
    37. NSLog(@"this is a class method");
    38. }
    39. - (void)printDZL
    40. {
    41. NSLog(@"this is a instance method");
    42. }
    43. @end

测试Demo: 

 

 

 

- (void)viewDidLoad {

[super viewDidLoad];
  1. // Do any additional setup after loading the view, typically from a nib.
  2. Person * p1 = [[Person alloc] init];
  3. Method m1 = class_getInstanceMethod([p1 class], @selector(printDZL));
  4. Method m2 = class_getClassMethod([Person class], @selector(printDZ));
  5. NSLog(@"测试前:");
  6.     [p1 printDZL];
  7.     [Person printDZ];
  8.     method_exchangeImplementations(m1, m2);
  9. NSLog(@"测试后:");
  10.     [p1 printDZL];
  11.     [Person printDZ];
  12. }


输出: 

 

 

  1.  
    2016-06-30 22:37:08.539 02-runtime[2776:69899] 测试前:
  2.  
    2016-06-30 22:37:08.539 02-runtime[2776:69899] this is a instance method
  3.  
    2016-06-30 22:37:08.539 02-runtime[2776:69899] this is a class method
  4.  
    2016-06-30 22:37:08.540 02-runtime[2776:69899] 测试后:
  5.  
    2016-06-30 22:37:08.540 02-runtime[2776:69899] this is a class method
  6.  
    2016-06-30 22:37:08.540 02-runtime[2776:69899] this is a instance method



 

 

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

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

  1.  
    - (void)viewDidLoad {
  2.  
    [super viewDidLoad];
  3.  
    // Do any additional setup after loading the view, typically from a nib.
  4.  
     
  5.  
    Person * p1 = [[Person alloc] init];
  6.  
     
  7.  
    // Method m1 = class_getInstanceMethod([p1 class], @selector(printDZL));
  8.  
    // Method m2 = class_getClassMethod([Person class], @selector(printDZ));
  9.  
    Method m1 = class_getInstanceMethod([Person class], @selector(printDZ));
  10.  
    Method m2 = class_getClassMethod([p1 class], @selector(printDZL));
  11.  
    NSLog(@"测试前:");
  12.  
    [p1 printDZL];
  13.  
    [Person printDZ];
  14.  
    method_exchangeImplementations(m1, m2);
  15.  
    NSLog(@"测试后:");
  16.  
    [p1 printDZL];
  17.  
    [Person printDZ];
  18.  
    }

输出:

 

  1.  
    2016-06-30 22:38:10.050 02-runtime[2793:70426] 测试前:
  2.  
    2016-06-30 22:38:10.051 02-runtime[2793:70426] this is a instance method
  3.  
    2016-06-30 22:38:10.051 02-runtime[2793:70426] this is a class method
  4.  
    2016-06-30 22:38:10.051 02-runtime[2793:70426] 测试后:
  5.  
    2016-06-30 22:38:10.051 02-runtime[2793:70426] this is a instance method
  6.  
    2016-06-30 22:38:10.051 02-runtime[2793:70426] this is a class method


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

 

 

参考: http://blog.csdn.net/lvdezhou/article/details/49636561

 

posted @ 2020-10-14 15:47  sundaysios  阅读(208)  评论(0)    收藏  举报