我想将常量UInt16
的值设置为十六进制值。
我知道设置Swift Character
的方法是
let myValue: Character = "\u{1820}"
所以我试过
let myValue: UInt16 = "\u{1820}"
let myValue: unichar = "\u{1820}"
let myValue: UInt16 = "\u{1820}".utf16
let myValue: unichar = "\u{1820}".utf16
let myValue = "\u{1820}"
但这些都没有奏效。
在搜索答案时,我大多有关于从NSString
或其他类型转换的问题。
对于所有经验丰富的Objective C程序员来说,这肯定是一个愚蠢的问题,但我很难找到答案。不过,我终于找到了,所以我将分享我的问题和答案,希望为将来可能会搜索相同问题的其他人添加一些关键字。
注意:
答案 0 :(得分:1)
如果您已经仔细阅读了Swift文档的The Basics,那么您会发现它。
整数文字可以写成:
- 十进制号,没有前缀
- 二进制号码,前缀为0b
- 八进制号码,前缀为0o
- 十六进制号码,带有0x前缀
所有这些整数文字的小数值都是17:
let decimalInteger = 17 let binaryInteger = 0b10001 // 17 in binary notation let octalInteger = 0o21 // 17 in octal notation let hexadecimalInteger = 0x11 // 17 in hexadecimal notation
所以你会这样做
let myValue: UInt16 = 0x1820
或
let myValue: unichar = 0x1820 // unichar is a type alias of UInt16