OC中isa指针详解 (union 联合体)

为什么要使用union联合体?

比如定义一个bool值变量需要8个字节,其实仅仅使用这8个字节中的一个位就可以表达的情况了,使用union就是充分利用每个字节的每一个位,节约空间。

本文主要介绍以下部分:
一. 自定义位运算
二. 自定义位域运算
三. 自定义联合体
四. isa解析

一. 自定义位运算

先看示例:

@interface Person : NSObject

- (void)setTall:(BOOL)tall;
- (void)setRich:(BOOL)rich;
- (void)setHandsome:(BOOL)handsome;

- (BOOL)isTall;
- (BOOL)isRich;
- (BOOL)isHandsome;

@end


#import "Person.h"

#define TallMask (1<<0)
#define RichMask (1<<1)
#define HandsomeMask (1<<2)

@interface Person()
{
    char _tallRichHansome;
}
@end

@implementation Person

- (instancetype)init
{
    if (self = [super init]) {
        _tallRichHansome = 0b00000000;
    }
    return self;
}

- (void)setTall:(BOOL)tall
{
    if (tall) {
        _tallRichHansome |= TallMask;
    } else {
        _tallRichHansome &= ~TallMask;
    }
}

- (BOOL)isTall
{
    return !!(_tallRichHansome & TallMask);
}

- (void)setRich:(BOOL)rich
{
    if (rich) {
        _tallRichHansome |= RichMask;
    } else {
        _tallRichHansome &= ~RichMask;
    }
}

- (BOOL)isRich
{
    return !!(_tallRichHansome & RichMask);
}

- (void)setHandsome:(BOOL)handsome
{
    if (handsome) {
        _tallRichHansome |= HandsomeMask;
    } else {
        _tallRichHansome &= ~HandsomeMask;
    }
}

- (BOOL)isHandsome
{
    return !!(_tallRichHansome & HandsomeMask);
}

@end

  1. 上边get方法使用!!取反两次是因为获取到的值可能是2、4、8,第一次取反变成0,再次取反变成1

main函数调用

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *person = [[Person alloc] init];
        person.rich = YES;
        person.tall = NO;
        person.handsome = NO;
        
        NSLog(@"tall:%d rich:%d hansome:%d", person.isTall, person.isRich, person.isHandsome);
    }
    return 0;
}

结果为:

tall:0 rich:1 hansome:0

二. 自定义位域运算

Person.m修改如下:

#import "Person.h"
@interface Person()
{
    struct {
        char tall : 1;
        char rich : 1;
        char handsome : 1;
    } _tallRichHandsome;
}
@end

@implementation Person

- (void)setTall:(BOOL)tall
{
    _tallRichHandsome.tall = tall;
}

- (BOOL)isTall
{
    return !!_tallRichHandsome.tall;
}

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

- (BOOL)isRich
{
    return !!_tallRichHandsome.rich;
}

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

- (BOOL)isHandsome
{
    return !!_tallRichHandsome.handsome;
}

@end
  1. 上边get方法使用!!取反两次是因为设置值为YES的时候二进制是这样的0b1,系统会自动把这个数据变成0b11111111结果为-1,因为该数据不为0,所以取反就会变成0,再次取反就会变成1
    或者修改如下:
 struct {
        unsigned char tall : 1;
        unsigned char rich : 1;
        unsigned char handsome : 1;
    } _tallRichHandsome;

即添加unsigned修饰符也可解决取值为-1的问题。

  1. tall 、rich 、 handsome 会从右至左各占一位。

三. 自定义联合体

Person.m修改如下:

#import "Person.h"

#define TallMask (1<<0)
#define RichMask (1<<1)
#define HandsomeMask (1<<2)
#define ThinMask (1<<3)

@interface Person()
{
    union {
        int bits;
        
        struct {
            char tall : 1;
            char rich : 1;
            char handsome : 1;
            char thin : 1;
        };
    } _tallRichHandsome;
}
@end

@implementation Person

- (void)setTall:(BOOL)tall
{
    if (tall) {
        _tallRichHandsome.bits |= TallMask;
    } else {
        _tallRichHandsome.bits &= ~TallMask;
    }
}

- (BOOL)isTall
{
    return !!(_tallRichHandsome.bits & TallMask);
}

- (void)setRich:(BOOL)rich
{
    if (rich) {
        _tallRichHandsome.bits |= RichMask;
    } else {
        _tallRichHandsome.bits &= ~RichMask;
    }
}

- (BOOL)isRich
{
    return !!(_tallRichHandsome.bits & RichMask);
}

- (void)setHandsome:(BOOL)handsome
{
    if (handsome) {
        _tallRichHandsome.bits |= HandsomeMask;
    } else {
        _tallRichHandsome.bits &= ~HandsomeMask;
    }
}

- (BOOL)isHandsome
{
    return !!(_tallRichHandsome.bits & HandsomeMask);
}



- (void)setThin:(BOOL)thin
{
    if (thin) {
        _tallRichHandsome.bits |= ThinMask;
    } else {
        _tallRichHandsome.bits &= ~ThinMask;
    }
}

- (BOOL)isThin
{
    return !!(_tallRichHandsome.bits & ThinMask);
}

@end

以下代码完全是增强可读性,删除掉完全不会有任何影响。

 struct {
            char tall : 1;
            char rich : 1;
            char handsome : 1;
            char thin : 1;
        };

通过上边代码的学习,就可以读懂OC底层isa指针的具体含义了。

四. isa解析

在arm64架构之前,isa就是一个普通的指针,存储着Class、Meta-Class对象的内存地址
从arm64架构开始,对isa进行了优化,变成了一个共用体(union)结构,还使用位域来存储更多的信息

union isa_t {
    Class cls;
    uintptr_t bits;
    struct {
        uintptr_t nonpointer        : 1;
        uintptr_t has_assoc         : 1;
        uintptr_t has_cxx_dtor      : 1;
        uintptr_t shiftcls          : 33; /*MACH_VM_MAX_ADDRESS 0x1000000000*/
        uintptr_t magic             : 6;
        uintptr_t weakly_referenced : 1;
        uintptr_t unused            : 1;
        uintptr_t has_sidetable_rc  : 1;
        uintptr_t extra_rc          : 19
    };
};

isa内部的位含义

  1. nonpointer
    0,代表普通的指针,存储着Class、Meta-Class对象的内存地址
    1,代表优化过,使用位域存储更多的信息

  2. has_assoc
    是否有设置过关联对象,如果没有,释放时会更快

  3. has_cxx_dtor
    是否有C++的析构函数(.cxx_destruct),如果没有,释放时会更快

  4. shiftcls
    存储着Class、Meta-Class对象的内存地址信息

  5. magic
    用于在调试时分辨对象是否未完成初始化

  6. weakly_referenced
    是否有被弱引用指向过,如果没有,释放时会更快
    deallocating
    对象是否正在释放

  7. extra_rc
    里面存储的值是引用计数器减1

  8. has_sidetable_rc
    引用计数器是否过大无法存储在isa中
    如果为1,那么引用计数会存储在一个叫SideTable的类的属性中

你可能感兴趣的:(OC中isa指针详解 (union 联合体))