我在将用户输入的十六进制字符串(即“1F436”)转换为我可以使用的 unicode 字符时遇到问题。
通过阅读 Swift 文档,我学习了如何使用 'print(\u{1F436})' 打印 unicode 字符,以及如何使用 for-in 循环计算出字符串中 unicode 字符的十进制值。
但是,如何从包含十六进制数字的字符串创建 unicode 字符变量呢?
目前我有这个:
let somePoint = (1, 0)
switch somePoint {
case (0,0):
print("origin") // does not print
fallthrough
case (_, 0):
print("y-axis") // prints y-axis. this makes sense
fallthrough
case(0, _):
print("x-axis") // prints x-axis (because of fallthrough? this should not print)
fallthrough
case(-2...2, -2...2):
print("in 5x5 box about the origin") // this prints and makes sense
default:
print("somewhere else") // does not print
}
Run Code Online (Sandbox Code Playgroud)
我的这个switch语句的目标是让每个case打印如果它是真的而不是只有第一个匹配print的case.我以为我可以通过这个演绎声明来做到这一点.但是,这让我怀疑它是如何工作的.即使案例不匹配,为什么fallthrough会自动打印下一个案例?我怎么能按照我想要的方式使这个switch语句工作?