OC基础 Runtime-isa (2)
2021-03-10 本文已影响0人
我是卖报的小行家
Runtime-isa 设值
isa之设值:
与运算 |
#import "ZKPerson.h"
//掩码:按位与运算
//#define MJTallMask 1
//#define MJRichMask 2
//#define MJHandsomeMask 4
//继续优化
//#define MJTallMask 0b00000001
//#define MJRichMask 0b00000010
//#define MJHandsomeMask 0b00000100
//继续优化
//此处拿到的1为 0b00000001
#define MJTallMask (1<<0) //拿到1左移0位取到0 相当于不移动还是0
#define MJRichMask (1<<1) //拿到1左移一位取到1 0b00000010
#define MJHandsomeMask (1<<2) //拿到1左移两位位取到2 0b00000100
@interface ZKPerson()
{
char _isTallRichHandsome;
}
@end
@implementation ZKPerson
- (instancetype)init
{
self = [super init];
if (self) {
_isTallRichHandsome = 0b00000110;
}
return self;
}
- (void)setTall:(BOOL)tall
{
if (tall) {
_isTallRichHandsome |= MJTallMask;
}else{
//按位与除了他的那一位为0,其他全为1(总结:每一位取反(~),按位与)
_isTallRichHandsome &= ~MJTallMask;
}
}
- (BOOL)tall
{
return !!(_isTallRichHandsome &MJTallMask);
}
- (void)setRich:(BOOL)rich
{
if (rich) {
_isTallRichHandsome |= MJRichMask;
}else{
//按位与除了他的那一位为0,其他全为1(总结:每一位取反(~),按位与)
_isTallRichHandsome &= ~MJRichMask;
}
}
- (BOOL)rich
{
return !!(_isTallRichHandsome &MJRichMask);
}
- (void)setHandsome:(BOOL)handsome
{
if (handsome) {
_isTallRichHandsome |= MJHandsomeMask;
}else{
//按位与除了他的那一位为0,其他全为1(总结:每一位取反(~),按位与)
_isTallRichHandsome &= ~MJHandsomeMask;
}
}
- (BOOL)handsome
{
return !!(_isTallRichHandsome &MJHandsomeMask);
}
@end
Main.m
ZKPerson *zk = [[ZKPerson alloc]init];
zk.tall = 0;
zk.rich = 1;
zk.handsome = 0;
NSLog(@"%d,%d,%d",zk.tall,zk.rich,zk.handsome);
//打印结果:2021-03-09 22:36:28.968453+0800 DoBlock[2072:2727939] 0,1,0
优化:位域
再次优化 使用结构体
@interface ZKPerson()
{
struct {
char tall:1;
char rich:1;
char handsome:1;
}_isTallRichHandsome;
}
@end
@implementation ZKPerson
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)setTall:(BOOL)tall
{
_isTallRichHandsome.tall = tall;
}
- (BOOL)tall
{
return !!_isTallRichHandsome.tall;
}
- (void)setRich:(BOOL)rich
{
_isTallRichHandsome.rich = rich;
}
- (BOOL)rich
{
return !!_isTallRichHandsome.rich;
}
- (void)setHandsome:(BOOL)handsome
{
_isTallRichHandsome.handsome = handsome;
}
- (BOOL)handsome
{
return !!_isTallRichHandsome.handsome;
}
@end
再次优化 union
#define MJTallMask (1<<0) //拿到1左移0位取到0 相当于不移动还是0
#define MJRichMask (1<<1) //拿到1左移一位取到1 0b00000010
#define MJHandsomeMask (1<<2) //拿到1左移两位位取到2 0b00000100
@interface ZKPerson()
{
union {
int bits;
struct{
char tall : 1;
char rich : 1;
char handsome : 1;
};
}_isTallRichHandsome;
}
@end
@implementation ZKPerson
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)setTall:(BOOL)tall
{
if (tall) {
_isTallRichHandsome.bits |= MJTallMask;
}else{
_isTallRichHandsome.bits &= ~MJTallMask;
}
}
- (BOOL)tall
{
return !!(_isTallRichHandsome.bits & MJTallMask);
}
- (void)setRich:(BOOL)rich
{
if (rich) {
_isTallRichHandsome.bits |= MJRichMask;
}else{
_isTallRichHandsome.bits &= ~MJRichMask;
}
}
- (BOOL)rich
{
return !!(_isTallRichHandsome.bits & MJRichMask);
}
- (void)setHandsome:(BOOL)handsome
{
if (handsome) {
_isTallRichHandsome.bits |= MJHandsomeMask;
}else{
_isTallRichHandsome.bits &= ~MJHandsomeMask;
}
}
- (BOOL)handsome
{
return !!(_isTallRichHandsome.bits & MJHandsomeMask);
}
@end