objc_msgSend调用实例方法,即使是所谓私有方法
~~~
// Dog.h
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@property(nonatomic, strong) NSString * dogName;
@property(nonatomic, assign) NSInteger dogAge;
@end
~~~
~~~
// Dog.m
#import "Dog.h"
@implementation Dog
- (instancetype)init
{
self = [super init];
if (self) {
self.dogName = @"dahuang";
self.dogAge = 2;
}
return self;
}
- (void) printDogName
{
NSLog(@"dogName");
}
@end
~~~
可以看到Dog类头文件中并没有printDogName方法的声明,所以Dog类的实例是不能访问到printDogName方法的,这就是所谓的私有方法,但并不是这个方法就不能再类外进行访问了,可以通过objc_msgSend
![](https://box.kancloud.cn/2016-05-05_572b015525a84.jpg)
虽然有警告说没有定义printDogName方法,但通过objc_msgSend方法还是访问到了。
- 前言
- object_getIvar
- object_getClass
- object_getClassName
- object_setClass
- object_isClass
- object_setIvar
- objc_getClass
- Too many arguments to function call...
- objc_msgSend
- class_copyIvarList和class_copyMethodList
- method_exchangeImplementations
- 运用SEL,运行时改变两个方法的实现
- class_getInstanceMethod和class_getClassMethod
- class_respondsToSelector
- Objective-C Runtime 运行时之三:方法与消息