是否有任何簡單的方法在Swift中將任何IntegerType轉換爲Int。我有一個包含不同類型整數的字典; UInt8,UInt16等,並需要將其轉換爲Int。有沒有除了switch語句的任何簡單的方法:Swift - 將任何IntegerType轉換爲Int
let x: UInt8 = 5
let any: Any = x
var size: Int
switch (any) {
case let value as Int8: size = Int(value)
case let value as UInt8: size = Int(value)
case let value as Int16: size = Int(value)
case let value as UInt16: size = Int(value)
case let value as Int32: size = Int(value)
case let value as UInt32: size = Int(value)
case let value as Int64: size = Int(value)
case let value as UInt64: size = Int(value)
case let value as Int: size = Int(value)
case let value as UInt: size = Int(value)
default: size = 0
}
你可以隨時使用任何IntegerType的hashValue屬性 –
這可能會工作,但是如何將Any轉換爲IntegerType? –
if let int =(any as?UInt8)?hashValue {print(int) } –