当前位置: 首页 > news >正文

卷帘门怎么做网站专注于网站营销服务

卷帘门怎么做网站,专注于网站营销服务,建设银行网站未响应,网页微博登录文章目录 iOS - Runtime-isa详解(位域、union(共用体)、位运算)前言1. 位域介绍1.1 思路1.2 示例 - 结构体1.3 示例 - union(共用体)1.3.1 说明 1.4 结构体 对比 union(共用体) 2. a…

文章目录

  • iOS - Runtime-isa详解(位域、union(共用体)、位运算)
    • 前言
    • 1. `位域`介绍
      • 1.1 思路
      • 1.2 示例 - 结构体
      • 1.3 示例 - union(共用体)
        • 1.3.1 说明
      • 1.4 结构体 对比 union(共用体)
    • 2. arm64架构对isa的优化
      • 2.1 位域内容
        • nonpointer
        • has_assoc
        • has_cxx_dtor
        • shiftcls
        • magic
        • weakly_referenced
        • deallocating
        • extra_rc
        • has_sidetable_rc
      • 2.2 Class、Meta-Class对象存储位置
    • 3. 拓展
      • 3.1 枚举值设计
      • 3.1.1 案例
      • 3.1.2 原理分析

iOS - Runtime-isa详解(位域、union(共用体)、位运算)

前言

本章主要了解Runtime相关内容,苹果对isa做了哪些优化,位域、union(共用体)又是如何运用的

  • 要想学习Runtime,首先要了解它底层的一些常用数据结构,比如isa指针
  • 在arm64架构之前,isa就是一个普通的指针,存储着ClassMeta-Class对象的内存地址
  • 从arm64架构开始,对isa进行了优化,变成了一个共用体(union)结构,还使用位域来存储更多的信息

1. 位域介绍

利用好位域的话,可以使程序运行高效节省内存,操作系统级别的东西很多都会使用

在IM系统的开发中,就使用了位域来对数据进行优化,后续有时间再抽出案例聊聊。

欢迎点赞,收藏,加关注!,谢谢你!!

1.1 思路

假如ZSXPerson类需要3个BOOL类型的属性,这时候我们通常会直接使用@property声明3个属性,这时候系统会给我们生成 3个_开头的成员变量,3对getset方法,所占据的内存也比较多

思路:BOOL 类型属性值要么 YES 要么 NO,使用字节中的一个(0或者1)其实就能表示一个 BOOL 类型的属性值,一个字节就可以表示8个 BOOL 值

1.2 示例 - 结构体

ZSXPerson.h

@interface ZSXPerson : NSObject- (void)setTall:(BOOL)tall;- (BOOL)isTall;- (void)setRich:(BOOL)rich;- (BOOL)isRich;- (void)setHandsome:(BOOL)handsome;- (BOOL)isHandsome;@end

ZSXPerson.m

@interface ZSXPerson() {struct {char tall: 1;char rich: 1;char handsome: 1;} _tallRichHandsome;
}@end@implementation ZSXPerson- (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

main.m

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

运行结果:

1.3 示例 - union(共用体)

ZSXPerson.h

@interface ZSXPerson : NSObject- (void)setTall:(BOOL)tall;- (BOOL)isTall;- (void)setRich:(BOOL)rich;- (BOOL)isRich;- (void)setHandsome:(BOOL)handsome;- (BOOL)isHandsome;@end

ZSXPerson.h.m

#import "ZSXPerson.h"#define ZSXTallMask (1)
#define ZSXRichMask (1 << 1)
#define ZSXHandsomeMask (1 << 2)@interface ZSXPerson() {union {char bits;struct {char tall: 1;char rich: 1;char handsome: 1;};}_tallRichHandsome;
}@end@implementation ZSXPerson- (void)setTall:(BOOL)tall {if (tall) {_tallRichHandsome.bits |= ZSXTallMask;}else {_tallRichHandsome.bits &= ~ZSXTallMask;}
}- (BOOL)isTall {return !!(_tallRichHandsome.bits & ZSXTallMask);
}- (void)setRich:(BOOL)rich {if (rich) {_tallRichHandsome.bits |= ZSXRichMask;}else {_tallRichHandsome.bits &= ~ZSXRichMask;}
}- (BOOL)isRich {return !!(_tallRichHandsome.bits & ZSXRichMask);
}- (void)setHandsome:(BOOL)handsome {if (handsome) {_tallRichHandsome.bits |= ZSXHandsomeMask;}else {_tallRichHandsome.bits &= ~ZSXHandsomeMask;}
}- (BOOL)isHandsome {return !!(_tallRichHandsome.bits & ZSXHandsomeMask);
}@end

main.m

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

运行结果:

1.3.1 说明

1.4 结构体 对比 union(共用体)

  • 结构体的成员是各自占用各自所需大小
  • 共同体的内存大小取决于其中最大的成员的大小,所有成员共用这块内存

  • 使用共用体实际上还是通过位运算来控制每个属性所占位置
  • 其中的sturct目的是增加可读性,实际上不会影响属性所占位置

2. arm64架构对isa的优化

arm64架构对isa中,使用一个64位的共用体来存储更多的信息,通过位域的概念来表示各个存储的信息的存储位置。其中有33位拿来存储Class、Meta-Class地址值


2.1 位域内容

nonpointer
  • 0,代表普通的指针,存储着Class、Meta-Class对象的内存地址
  • 1,代表优化过,使用位域存储更多的信息
has_assoc
  • 是否有设置过关联对象,如果没有,释放时会更快
has_cxx_dtor
  • 是否有C++的析构函数(.cxx_destruct),如果没有,释放时会更快
shiftcls
  • 存储着Class、Meta-Class对象的内存地址信息
magic
  • 用于在调试时分辨对象是否未完成初始化
weakly_referenced
  • 是否有被弱引用指向过,如果没有,释放时会更快
deallocating
  • 对象是否正在释放
extra_rc
  • 里面存储的值是引用计数器减1
has_sidetable_rc
  • 引用计数器是否过大无法存储在isa中
  • 如果为1,那么引用计数会存储在一个叫SideTable的类的属性中

2.2 Class、Meta-Class对象存储位置

Class、Meta-Class对象存储在shiftcls,从上图可知shiftcls是从第4位开始,连续33

isa的掩码:define ISA_MASK 0x0000000ffffffff8ULL

将掩码转为二进制查看

它表示的就是从第4位开始,连续33位,通过这个掩码,就可以将Class、Meta-Class地址值取出来

Class、Meta-Class地址值后三位永远是 0。因为他的掩码左右边 3位是 0,&运算后一定是 0

3. 拓展

3.1 枚举值设计

在iOS中,系统的一些API可以使用|传入多个枚举值,比如:

self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;

原理就是使用位域设计枚举值,然后通过位运算来取值

3.1.1 案例

我们也自己来设计一个这样的枚举

定义枚举:

typedef NS_OPTIONS(NSUInteger, ZSXOptions) {ZSXOptions1                 = 1 << 0, // 0b00000001ZSXOptions2                 = 1 << 1, // 0b00000010ZSXOptions3                 = 1 << 2, // 0b00000100ZSXOptions4                 = 1 << 3, // 0b00001000ZSXOptions5                 = 1 << 4, // 0b00010000
};

设置值方法:

- (void)setOptions:(ZSXOptions)options {if (options & ZSXOptions1) {NSLog(@"包含了ZSXOptions1");}if (options & ZSXOptions2) {NSLog(@"包含了ZSXOptions2");}if (options & ZSXOptions3) {NSLog(@"包含了ZSXOptions3");}if (options & ZSXOptions4) {NSLog(@"包含了ZSXOptions4");}if (options & ZSXOptions5) {NSLog(@"包含了ZSXOptions5");}
}

使用:

[self setOptions:ZSXOptions1 | ZSXOptions3 | ZSXOptions5];

打印如下:

此时我们已经实现了一个可以传入多个枚举值的接口

3.1.2 原理分析

/**0b000000010b000001000b00010000----------------- | 运算(设置值)0b000101010b00000001----------------- & 运算(取值)0b00000001    为 true*/

@oubijiexi


文章转载自:
http://superphosphate.hwbf.cn
http://nielsbohrium.hwbf.cn
http://iab.hwbf.cn
http://majuscule.hwbf.cn
http://bushwalking.hwbf.cn
http://cims.hwbf.cn
http://labradorite.hwbf.cn
http://autocatalytic.hwbf.cn
http://dobber.hwbf.cn
http://using.hwbf.cn
http://steeply.hwbf.cn
http://mastika.hwbf.cn
http://androgenous.hwbf.cn
http://animal.hwbf.cn
http://ware.hwbf.cn
http://snowball.hwbf.cn
http://samlor.hwbf.cn
http://hairline.hwbf.cn
http://indeterminacy.hwbf.cn
http://viciousness.hwbf.cn
http://cornetcy.hwbf.cn
http://bhn.hwbf.cn
http://doublethink.hwbf.cn
http://treelined.hwbf.cn
http://epistoma.hwbf.cn
http://ademption.hwbf.cn
http://submarginal.hwbf.cn
http://successivity.hwbf.cn
http://wantonness.hwbf.cn
http://fabianist.hwbf.cn
http://pupiform.hwbf.cn
http://irascibly.hwbf.cn
http://illinoisan.hwbf.cn
http://mimbar.hwbf.cn
http://suborder.hwbf.cn
http://rarebit.hwbf.cn
http://intercrop.hwbf.cn
http://dubitable.hwbf.cn
http://subtilisin.hwbf.cn
http://repeatedly.hwbf.cn
http://remainderman.hwbf.cn
http://aquamarine.hwbf.cn
http://absent.hwbf.cn
http://fuguist.hwbf.cn
http://award.hwbf.cn
http://ghastly.hwbf.cn
http://corticated.hwbf.cn
http://assuring.hwbf.cn
http://unstrap.hwbf.cn
http://gallooned.hwbf.cn
http://taenia.hwbf.cn
http://morphotactics.hwbf.cn
http://monoaminergic.hwbf.cn
http://imperator.hwbf.cn
http://engird.hwbf.cn
http://assonance.hwbf.cn
http://stundism.hwbf.cn
http://annihilator.hwbf.cn
http://cassimere.hwbf.cn
http://boggy.hwbf.cn
http://numeration.hwbf.cn
http://landtax.hwbf.cn
http://ghastfulness.hwbf.cn
http://metestrum.hwbf.cn
http://bus.hwbf.cn
http://kinglet.hwbf.cn
http://heist.hwbf.cn
http://rediscount.hwbf.cn
http://aniseikonic.hwbf.cn
http://box.hwbf.cn
http://kraurosis.hwbf.cn
http://underboss.hwbf.cn
http://homopolarity.hwbf.cn
http://abrogate.hwbf.cn
http://cellobiose.hwbf.cn
http://syndicalism.hwbf.cn
http://tempering.hwbf.cn
http://pressman.hwbf.cn
http://steeplejack.hwbf.cn
http://countian.hwbf.cn
http://pungi.hwbf.cn
http://metaphrase.hwbf.cn
http://unfavorably.hwbf.cn
http://snorter.hwbf.cn
http://weariness.hwbf.cn
http://assignee.hwbf.cn
http://plunk.hwbf.cn
http://adversary.hwbf.cn
http://hodometer.hwbf.cn
http://vitrescent.hwbf.cn
http://tachometer.hwbf.cn
http://carbachol.hwbf.cn
http://hard.hwbf.cn
http://worthful.hwbf.cn
http://reprobatively.hwbf.cn
http://milchig.hwbf.cn
http://enfleurage.hwbf.cn
http://ambilingnal.hwbf.cn
http://calligraphic.hwbf.cn
http://pygal.hwbf.cn
http://www.15wanjia.com/news/73820.html

相关文章:

  • 做搜狗网站优化首windows优化大师有必要安装吗
  • 福州市工程建设监督站网站投放广告找什么平台
  • 万网站手机百度云电脑版入口
  • 设计企业网站首页网络品牌推广
  • wordpress 头像 插件seo免费外链工具
  • 深圳网站建设制作培训网站seo诊断报告怎么写
  • 老百姓可以做监督政府的网站吗宁波seo推广公司排名
  • 做网站哪家专业阿里云搜索引擎
  • 高端网站建设优化网络营销最基本的应用方式是什么
  • 东莞做网站优化google优化排名
  • 我要表白网站云盘搜索
  • 在线手机网站预览看b站二十四小时直播间
  • 怎么用net123做网站外贸谷歌seo
  • 做网站用的云控制台bt磁力在线种子搜索神器
  • 临清网站建设网页设计与制作用什么软件
  • 博彩网站怎么做成人短期技能培训学校
  • asp网站配色网络营销的概念和特点是什么
  • 建设网站排名靠前找个网站
  • 哈尔滨有多少家网站建设公司培训机构招生7个方法
  • 用ps怎么做短视频网站百度竞价排名
  • 自有服务器可以做网站吗产品推广词
  • 东莞建设网站培训上海seo推广平台
  • 怎么进网站淘宝推广运营
  • 四川做网站的公司网站关键词快速优化
  • 手机网站域名开头seo平台优化服务
  • 最新新闻热点素材seo指导
  • php租车网站sem是什么意思呢
  • 网站建设有什么系统批量查询神马关键词排名
  • 做网站费用列入什么科目人员优化方案
  • 网站域名注册商标百度seo整站优化