This is my listing:
enum Object: Int{ case House1 = 0 case House2 = 1 var descriptor:String{ switch self{ case .House1: return "Cottage" case .House2: return "House" } } }
I want to know if there is a way to return rawValueif I provide a handle value?
rawValue
For example, I want to know the value of Enum, if my line is "Cottage", (It should return 0)
How can i achieve this?
You can create an initializer for your enumeration that takes a descriptor and returns an enumeration value for it, and then just call enumValue.rawValue. See the following:
enumValue.rawValue
enum Object: Int{ case House1 = 0 case House2 = 1 var descriptor:String{ switch self{ case .House1: return "Cottage" case .House2: return "House" } } init(descriptor: String) { switch descriptor { case "Cottage": self = .House1 case "House": self = .House2 default: self = .House1 // Default this to whatever you want } } }
Now do something like let rawVal = Object(descriptor: "House").rawValue
let rawVal = Object(descriptor: "House").rawValue
, . , , ?
enum Object: String { case House1 = "Cottage" case House2 = "House" }
, , , .
, , House1 0, "Cottage", , . , , , , . , :
0
"Cottage"
["Cottage", "House"]
0 "Cottage" (.. ).
enum Object: Int{ case House1 = 0 case House2 = 1 var description: String { return descriptor } var descriptor:String{ switch self{ case .House1: return "Cottage" case .House2: return "House" } } static func valueFor(string:String) -> Int{ switch string{ case "Cottage": return 0 case "House": return 1 default: return 2 } } } let obj = Object.House1 print(obj.description) print(obj.rawValue) print(Object.valueFor(string: "Cottage") //Output Cottage 0 0
Source: https://habr.com/ru/post/1659609/More articles:How to configure clang format without .clang-format file in each workspace? - visual-studio-codeСосредоточьтесь на определенной строке после использования ng-repeat - javascriptPyinstaller freezes with the addition of Microsoft.VC90.MFC redirect - pythonConverting a list of models to a map with an internal map using Java 8 streams - javaReplace dot with underscore in js object key names - javascriptUnresolved Dependency for Spark 2.0.1 on SBT - scalahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1659611/what-does-purge-local-repository-actually-purge&usg=ALkJrhjhpr1J0IIt-iCqS9lmT7Rg7j1t1whttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1659612/c-cross-platform-gui-for-desktop-and-mobile&usg=ALkJrhhnhjz-HkDAkOBhwn3nl7ApPN6VoQSonarQube false positive squid: S1450 for annotated fields @Getter (lombok) - javaBandwidth Limitations Using Nginx Using Amazon S3 - amazon-s3All Articles