一、Class
在之前的文章中我们提到,所有的对象都有个isa指针指向它对应的类Class,而Class是一个objc_class结构体,结构体中:
- 实例变量列表objc_ivar_list
- 方法列表objc_method_list
对应的runtime获取方法:
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
首先我们先创建一个Test类:
//
// Test.h
// RuntimeKVC
//
// Created by z on 2019/5/24.
// Copyright © 2019年 com.jzsec. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Test : NSObject
{
@public
NSString *name;
/* 头文件中定义私有变量,默认为@protected */
@private
NSString *major;
}
@property (nonatomic, strong) Test *childTest;
@end
NS_ASSUME_NONNULL_END
//
// Test.m
// RuntimeKVC
//
// Created by z on 2019/5/24.
// Copyright © 2019年 com.jzsec. All rights reserved.
//
#import "Test.h"
@interface Test ()
{
/* 类扩展区域定义私有变量,默认j就是@private */
int age;
}
/* 类扩展区域定义私有属性 */
@property (nonatomic, copy) NSString *job;
/* 类扩展区域定义私有变量,默认j就是@private */
- (void)test;
@end
@implementation Test
-(void)test
{
NSLog(@"这是个私有实例方法");
}
@end
二、Runtime访问
runtime暴力访问私有属性、私有变量和私