原创 OC底层 - runtime 结构体+位域

使用结构体位域:

#import "Student.h"

@interface Student()
{
    //一个字节   可以表示8个BOOL变量
    /**
        位域
     */
    struct{
        char tial : 1;
        char rich : 1;
        char handsome : 1;
    } _tailRichHandsome;
}
 
@end

@implementation Student


-(BOOL)getTail{
    //取出一位, 不能直接转为BOOL类型  作为一个取反才做   位域使用一位的时候, 如果强转为BOOL, 会将前边的位的值填充为1;
    return !!_tailRichHandsome.tial;
}
 
-(void)setTail:(BOOL)tail{
    _tailRichHandsome.tial = tail;
}

-(BOOL)getRich{
    return !!_tailRichHandsome.rich;
}

-(void)setRich:(BOOL)rich{
    _tailRichHandsome.rich = rich;
}

-(BOOL)getHandsome{
    return !!_tailRichHandsome.handsome;
}

-(void)setHandsome:(BOOL)handsome{
    _tailRichHandsome.handsome = handsome;
}


@end

猜你喜欢

转载自blog.csdn.net/Batac_Lee/article/details/110818020