有一个来自 Int 类型的多个案例的枚举,我如何通过它的 rawValue 打印案例名称?
public enum TestEnum : UInt16{
case ONE = 0x6E71
case TWO = 0x0002
case THREE = 0x0000
}
Run Code Online (Sandbox Code Playgroud)
我通过 rawValue 访问枚举:
print("\nCommand Type = 0x" + String(format:"%02X", someObject.getTestEnum.rawValue))
/*this prints: Command Type = 0x6E71
if the given Integer value from someObject.TestEnum is 28273*/
Run Code Online (Sandbox Code Playgroud)
现在我还想在十六进制值后打印“ONE”。
我知道这个问题:如何在 Swift 中获取枚举值的名称? 但这是不同的,因为我想通过案例原始值而不是枚举值本身来确定案例名称。
期望输出:
命令类型 = 0x6E71,一
我想根据表元素的条目索引提供实际页面。我用PageRequest.of(int page, int size).
我有index传递给函数的 ,以及一个PAGESIZE_SEARCH50的常量。
我想根据索引传递当前页面如下:
PageRequest.of((int) index/PAGESIZE_SEARCH, PAGESIZE_SEARCH)
Run Code Online (Sandbox Code Playgroud)
我的 IDEA(IntelliJ Ultimate 2020.1.1)使我的投射和暗示变灰 Casting 'index' to 'int' is redundant。如果我删除演员表,即使编译器接受它,我也不会收到运行时异常。
为什么是这样?那里的部门不是不安全吗?任何解释将不胜感激。