iOS underlying learning -day-11

Introduction -OC-Rutime articles

I am an iOS developer, iOS underlying rookie advanced road for 30 days.

isa pointer

  • To learn Runtime, we must first understand its underlying some commonly used data structures, such as isa pointer
  • Before arm64 architecture, isa is an ordinary pointer, stores Class, Meta-Class object memory address
  • Starting arm64 architecture is optimized for isa, into a common body (Union) structure, bit field used to store more information

Bit field

  • MJPerson.h
#import <Foundation/Foundation.h>

@interface MJPerson : NSObject

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

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

@end
  • MJPerson.m
#import "MJPerson.h"

#define MJTallMask (1<<0) 0b 0000 0001
#define MJRichMask (1<<1) 0b 0000 0010
#define MJHandsomeMask (1<<2) 0b 0000 0100

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

@implementation MJPerson

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

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

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

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

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

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

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

@end

Optimized

  • MJPerson.h
#import <Foundation/Foundation.h>

@interface MJPerson : NSObject

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

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

@end
  • MJPerson.m
#import "MJPerson.h"

@interface MJPerson()
{
    // 位域
    struct {
        char tall : 1;
        char rich : 1;
        char handsome : 1;
    } _tallRichHandsome; //0b 0000 0 handsome rich tall
}
@end

@implementation MJPerson

- (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

isa_max

  • From arm64, optimized isa, isa & isa_max is pointing object is the address value, take a common structural body, the structural body 64 to a data store something more, which is shift_cls33 bits to store specific address worth it
Published 12 original articles · won praise 0 · Views 97

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/103946606