Vivek
Vivek

Reputation: 5223

Get enum value as Int in swift

How to get enum Int value in swift

Create Enum

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

Print without hash (Perfect pass variable)

po "\(friendActionType)"
"deleteFriendRequest"

Print HashValue Log (Print wrong int value '-1188319181990702728' instead of '5')

po friendActionType.hashValue
-1188319181990702728

I want Int '5' value from enum variable.

Upvotes: 1

Views: 292

Answers (1)

Abhiraj Kumar
Abhiraj Kumar

Reputation: 143

Use friendActionType.rawValue

example of rawvalue

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

Upvotes: 4

Related Questions