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

雖然有警告說沒有定義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 運行時之三:方法與消息