在arm64之后, 优化了isa指针实现, 使用共同体实现isa, 可以节省内存:
以下使用一个char变量保存多个BOOL变量的值, 实现代码:
///左移操作
#define LPTail (1<<0)
#define LPRich (1<<1)
#define LPHandsome (1<<2)
//
// Person.m
// demo007_runtime_isa_共用体
//
// Created by Batac on 2020/12/7.
//
#import "Person.h"
@interface Person()
{
//一个字节 可以表示8个BOOL变量
char _tailRichHandsome;
}
@end
@implementation Person
-(BOOL)getTail{
return !!(_tailRichHandsome & LPTail);
}
-(void)setTail:(BOOL)tail{
if (tail) {
_tailRichHandsome = _tailRichHandsome | LPTail;
}else{
_tailRichHandsome = _tailRichHandsome & ~LPTail;
}
}
-(BOOL)getRich{
return !!(_tailRichHandsome & LPRich);
}
-(void)setRich:(BOOL)rich{
if (rich) {
_tailRichHandsome = _tailRichHandsome | LPRich;
}else{
_tailRichHandsome = _tailRichHandsome & ~LPRich;
}
}
-(BOOL)getHandsome{
return !!(_tailRichHandsome & LPHandsome);
}
-(void)setHandsome:(BOOL)handsome{
if (handsome) {
_tailRichHandsome = _tailRichHandsome | LPHandsome;
}else{
_tailRichHandsome = _tailRichHandsome & ~LPHandsome;
}
}
@end