位与运算在iOS中的使用

来源:互联网 发布:图片无缝拼接软件 编辑:程序博客网 时间:2024/05/17 22:34

先看一段我们常见的代码

<span style="font-size:14px;">UIView *view = [[UIView alloc]init];view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;[self.view addSubview:view];</span>

很多初学者明白这句代码的意思是  保证与superView左边和右边的距离不变,顶部和底部的距离不变。
<span style="font-size:14px;">view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;</span>
但是细看发现 autoresizingMask 的类型是 NSUInteger 

<span style="font-size:14px;">typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {    UIViewAutoresizingNone                 = 0,    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,    UIViewAutoresizingFlexibleWidth        = 1 << 1,    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,    UIViewAutoresizingFlexibleHeight       = 1 << 4,    UIViewAutoresizingFlexibleBottomMargin = 1 << 5};</span>

<<  就是左移运算符  a << 4  只是把a的各二进位左移4位

UIViewAutoresizingFlexibleWidth = 1 << 1  相当于    0001 << 1 =  00010 

UIViewAutoresizingFlexibleHeight = 1 << 4  相当于   0001 << 4 =  10000

再介绍一下 或运算   |        只要对应的二个二进位有一个为1时,结果位就为1

00001001

|

00000101

=

00001101

当我们写UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 时,00010 |10000 = 10010. 实际就是

那我么如何知道,autoresizingMask 选中了 这个2个选项呢?

需要用到 与运算 &只有对应的二个二进位都为1时,结果位才是1

<span style="font-size:14px;">if(view.autoresizingMask & UIViewAutoresizingFlexibleWidth){                 NSLog(@"UIViewAutoresizingFlexibleWidth 被选中了,需要保证与superView左边和右边的距离不变");    }        if(view.autoresizingMask & UIViewAutoresizingFlexibleHeight){                 NSLog(@"UIViewAutoresizingFlexibleHeight 被选中了,需要保证与superView顶部和底部的距离不变");    }        if(view.autoresizingMask & UIViewAutoresizingFlexibleLeftMargin){                NSLog(@"UIViewAutoresizingFlexibleLeftMargin 被选中了,需要保证与superView右边的距离不变");    }</span>

若是运行上面的代码,则只会打印 line1 和 line2 ,不会打印line3

我么知道   autoresizingMask  = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight =00010 |10000 = 10010 。

1 view.autoresizingMask & UIViewAutoresizingFlexibleWidth = 10010 &00010 = 00010 = 2. 所以为真 line1执行

2view.autoresizingMask & UIViewAutoresizingFlexibleHeight = 10010 & 10000 = 10000 = 16 同样为真 line2 执行

3  UIViewAutoresizingFlexibleLeftMargin = 1 << 0 = 00001 << 0 = 00001

view.autoresizingMask & UIViewAutoresizingFlexibleLeftMargin = 100010 & 00001 = 00000 = 0 为假, 所以line3不会被执行。


所以,我么只需要稍微理解 3个运算符 (& | <<) 就能很好理解

<span style="font-size:14px;">view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;</span>

这样写的原因和好处了。

在实际应用中,我们也可以模仿这样一种写法。例如在图文混排时,我么通常需要在一段文字中,检测是否有网络地址URL, 是否有表情符号Emoji,是否有电话号码,,是否有邮箱等等。

<span style="font-size:14px;">typedef NS_OPTIONS(NSUInteger, FWDetectorTypes) {        FWDetectorTypesNone = 0,    FWDetectorTypesURL           = 1 << 0,  //1    FWDetectorTypesEmoji         = 1 << 1,  //2    FWDetectorTypesPhoneNumber   = 1 << 2,  //4    FWDetectorTypesEmail         = 1 << 3,  //8};</span>
可以这样定义。
<span style="font-size:14px;">/**  需要识别的类型:默认识别表情 **/@property(nonatomic,assign)FWDetectorTypes types;</span>

如果我们只需要识别表情 types =FWDetectorTypesEmoji;

如果我们需要同时识别表情,电话,链接types = FWDetectorTypesEmoji |FWDetectorTypesURL|FWDetectorTypesPhoneNumber

在内容实现中,我么可以通过

types&FWDetectorTypesEmoji 来判断是否需要 执行识别表情的代码

types&FWDetectorTypesPhoneNumber 来判断 是否需要识别电话号码 等等。


其实上面所有的写法,本质就是   & |  << 这三个符号的运算。根据运算后的结果是否大于0,来进行判断。












0 0
原创粉丝点击