3 ms·
Swift: How to make single-character Strings work in a UInt8 raw value enum
- icodestuff 11y agoThe motivation for this was trying to parse a binary file with a discriminated union, with the tag being a single byte representing one of several ASCII characters. Since Swift doesn't have single byte character literals like single-quoted characters in C, UInt8 raw value enums required decimal or hexadecimal integer literals, which really hurts readability; 0x42 is strictly worse than 'B'. As a StringLiteralConvertible, "B" is an acceptable value for a UInt8 case.