为什么在iOS枚举中使用十六进制而不是十进制?

时间:2018-12-19 08:41:33

标签: c++ ios c

typedef NS_OPTIONS (NSInteger, YYTextLineStyle) {
// basic style (bitmask:0xFF)
YYTextLineStyleNone       = 0x00, ///< (        ) Do not draw a line (Default).
YYTextLineStyleSingle     = 0x01, ///< (──────) Draw a single line.
YYTextLineStyleThick      = 0x02, ///< (━━━━━━━) Draw a thick line.
YYTextLineStyleDouble     = 0x09, ///< (══════) Draw a double line.

// style pattern (bitmask:0xF00)
YYTextLineStylePatternSolid      = 0x000, ///< (────────) Draw a solid line (Default).
YYTextLineStylePatternDot        = 0x100, ///< (‑ ‑ ‑ ‑ ‑ ‑) Draw a line of dots.
YYTextLineStylePatternDash       = 0x200, ///< (— — — —) Draw a line of dashes.
YYTextLineStylePatternDashDot    = 0x300, ///< (— ‑ — ‑ — ‑) Draw a line of alternating dashes and dots.
YYTextLineStylePatternDashDotDot = 0x400, ///< (— ‑ ‑ — ‑ ‑) Draw a line of alternating dashes and two dots.
YYTextLineStylePatternCircleDot  = 0x900, ///< (••••••••••••) Draw a line of small circle dots.
};

这是我在框架上看到的代码,通常枚举值使用十进制,但是此代码使用十六进制,有什么好处?

3 个答案:

答案 0 :(得分:2)

只能看到这些位:

以十六进制格式,您可以轻松看到位:

0x0100 | 0x11 = 0x0111

用小数表示的不是:

256 | 17 = 273

答案 1 :(得分:0)

答案选项a:
 询问作者。

答案b(我猜):
 这是作者认为的可读性。
他们可能会考虑以低字节位掩码(在注释中的()内部)为背景/附近的值,即使它们没有通过最低半字节,也更易于读取。在第二组值(与高字节位掩码和DO传递低字节有关)的情况下,对所有值使用十六进制更加一致,因此有助于提高可读性。

答案 2 :(得分:0)

使用位,然后您可以通过按位或enum操作来组合多个|

当可以同时存在多个正交属性时,这很有用。

例如,YYTextLineStyleThick | YYTextLineStylePatternDot将用虚线绘制一条粗线。