[swift] int to enum 변환

천현철·2022년 4월 26일
0

iOS

목록 보기
50/51
post-custom-banner

int to enum:

public enum LanguageCode : Int{
    case ko
    case en
    case ja
    case zhcn
    case zhtw
    case th
    case vi
    case es
    case pt
    case fr
    case de
    case ru
    
    var description: String {
        return String(describing: self)
    }
}

print(languageCode.rawValue) //0
print(languageCode.description) //ko

string to enum:

public enum LoginType: String {
    case google="GOOGLE"
    case facebook="FACEBOOK"
    case apple="APPLE"
    case guest="GUEST"
}

print(LoginType.google.rawValue) //GOOGLE

출처
https://stackoverflow.com/questions/28461133/swift-enum-both-a-string-and-an-int
https://stackoverflow.com/questions/24701075/swift-convert-enum-value-to-string

profile
기도하지말고 행동하라
post-custom-banner

0개의 댓글