注册

类的布局——成员变量

日常开发中,我们定义的OC类,都会被编译成结构体类型:

/// Represents an instance of a class.
struct objc_object {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
};

在类中定义的属性和成员变量,也会变成结构体的成员变量:

@interface ObjectA : NSObject
@property(nonatomic,assign)BOOL b;
@end

// 类ObjectA会转化为如下结构体
struct ObjectA_IMPL {
struct NSObject_IMPL NSObject_IVARS; // 继承自NSObject
BOOL _b;
};

知道了类的真面目,可以在内存级别去做一些操作;例如:

@interface ObjectA : NSObject
@property(nonatomic,assign,readonly)BOOL b;
@end

...

- (void)viewDidLoad {
[super viewDidLoad];
ObjectA *aObj = [[ObjectA alloc] init];

NSLog(@"%@",aObj); // 在这一行打断点

// Do any additional setup after loading the view.
}

ca0e34e43d19427a3c286924759426e0.jpg

用代码实现如下:

- (void)viewDidLoad {
[super viewDidLoad];
ObjectA *aObj = [[ObjectA alloc] init];

void *aObj_ptr = (__bridge void *)aObj;
BOOL *_b_ptr = (BOOL *)((BytePtr)aObj_ptr + 0x8);
*_b_ptr = YES;

NSLog(@"%d",aObj.b); // 打印:1 证明已修改

// Do any additional setup after loading the view.
}


0 个评论

要回复文章请先登录注册