objc_msgSend

来源:互联网 发布:常见办公软件 编辑:程序博客网 时间:2024/05/18 03:44

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方法还是访问到了。





0 0
原创粉丝点击