快速获取Int枚举值

时间:2019-07-17 04:15:46

标签: swift enums

如何快速获取枚举Int值

创建枚举

enum FriendActionType:Int {    
    case sendFriendRequest = 1, cancelFriendRequest, acceptFriendRequest, removeFromFriendList, deleteFriendRequest
}

不带散列的打印(完美的传递变量)

po "\(friendActionType)"
"deleteFriendRequest"

打印HashValue日志(打印错误的int值'-1188319181990702728'而不是'5')

po friendActionType.hashValue
-1188319181990702728

我要从枚举变量中获取Int'5'值。

1 个答案:

答案 0 :(得分:3)

使用friendActionType.rawValue

原始值示例

 enum Planet: Int {
        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
    }
    let earthsOrder = Planet.earth.rawValue
    // earthsOrder is 3