iOS位运算实例

completionHandler(UNNotificationPresentationOptionBadge|
                                  UNNotificationPresentationOptionSound|
                                  UNNotificationPresentationOptionAlert);

这段代码是不是很眼熟?

这段代码在做APNS推送时有用到,需要设置推送的提醒方式,这里的意思是,提醒方式为:徽标或声音或提示框。

以下代码就是我们今天要讲到的内容,位运算的一个例子,可以尝试打印,控制台输出结果是:7

NSLog(@"%lu",UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound| UNNotificationPresentationOptionAlert)

这个结果是怎么来的呢?

按住 option 键,将鼠标移动到 UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound| UNNotificationPresentationOptionAlert 上面,可以看到如下:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
    UNNotificationPresentationOptionBadge   = (1 << 0),
    UNNotificationPresentationOptionSound   = (1 << 1),
    UNNotificationPresentationOptionAlert   = (1 << 2),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

UNNotificationPresentationOptionBadge = (1 << 0) : 表示00000001左移0位 = 00000001

UNNotificationPresentationOptionSound = (1 << 1):表示00000001左移1位 = 00000010

UNNotificationPresentationOptionAlert = (1 << 2) :表示00000001左移2位 = 00000100

位运算怎么计算,这里就不多说了,自己可以谷歌搜一下

这里|或运算,也就是:00000001 | 00000010 | 00000100 = 00000111 = 4 + 2 + 1 = 7

tips: 二进制转十进制的口算其实很简单,以一个8bit的二进制数为例子:1111 1111,从右往左,“1”分别表示十进制的:1,2,4,8,16,32,64,128。也就是哪1位有1就将该位对应的十进制数相加即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容