How to get enum from raw value in Swift?

前端 未结 7 1004
一向
一向 2020-12-13 23:25

I\'m trying to get enum type from raw value:

enum TestEnum: String {
    case Name
    case Gender
    case Birth

    var rawValue: String {
        switch          


        
相关标签:
7条回答
  • 2020-12-14 00:05

    Too complicated, just assign the raw values directly to the cases

    enum TestEnum: String {
      case Name = "Name"
      case Gender = "Gender"
      case Birth = "Birth Day"
    }
    
    let name = TestEnum(rawValue: "Name")!       //Name
    let gender = TestEnum(rawValue: "Gender")!   //Gender
    let birth = TestEnum(rawValue: "Birth Day")! //Birth
    

    If the case name matches the raw value you can even omit it

    enum TestEnum: String {
      case Name, Gender, Birth = "Birth Day"
    }
    

    In Swift 3+ all enum cases are lowercased

    0 讨论(0)
  • 2020-12-14 00:06

    You can define enum like this -

    enum TestEnum: String {
        case Name, Gender, Birth
    }
    

    OR

    enum TestEnum: String {
        case Name
        case Gender
        case Birth
    }
    

    you can provide an init method which defaults to one of the member values.

    enum TestEnum: String {
        case Name, Gender, Birth
    
        init() {
            self = .Gender
        }
    }
    

    In the example above, TestEnum.Name has an implicit raw value of "Name", and so on.

    You access the raw value of an enumeration case with its rawValue property:

    let testEnum = TestEnum.Name.rawValue
    // testEnum is "Name"
    let testEnum1 = TestEnum() 
    // testEnum1 is "Gender"
    
    0 讨论(0)
  • 2020-12-14 00:07

    Display the rawvalue using Enum

    import UIKit
    
    enum car: String {
        case bmw =  "BMW"
        case jaquar = "JAQUAR"
        case rd = "RD"
        case benz = "BENZ"
    
    }
    
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var label: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            label.text = car.bmw.rawValue
    
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-14 00:12

    Full working example:

    enum TestEnum: String {
        case name = "A Name"
        case otherName
        case test = "Test"
    }
    
    let first: TestEnum? = TestEnum(rawValue: "A Name")
    let second: TestEnum? = TestEnum(rawValue: "OtherName")
    let third: TestEnum? = TestEnum(rawValue: "Test")
    
    print("\(first), \(second), \(third)")
    

    All of those will work, but when initializing using a raw value it will be an optional. If this is a problem you could create an initializer or constructor for the enum to try and handle this, adding a none case and returning it if the enum couldn't be created. Something like this:

    static func create(rawValue:String) -> TestEnum {
            if let testVal = TestEnum(rawValue: rawValue) {
                return testVal
            }
            else{
                return .none
            }
        }
    
    0 讨论(0)
  • 2020-12-14 00:16

    I think this is a quick and clean solution for swift 4.2 (you can c&p to playground)

    import UIKit
    
    public enum SomeEnum: String, CaseIterable {
        case sun,moon,venus,pluto
    }
    
    let str = "venus"
    let newEnum = SomeEnum.allCases.filter{$0.rawValue == str}.first
    // newEnum is optional
    if let result = newEnum {
        print(result.rawValue)
    }
    
    0 讨论(0)
  • 2020-12-14 00:20

    Here is example of more useable code in swift 4.1

    import UIKit
    
    enum FormData {
      case userName
      case password
    
      static let array = [userName, password]
    
      var placeHolder: String {
        switch self {
        case .userName:
          return AppString.name.localized // will return "Name" string
        case .password:
          return AppString.password.localized // will return "Password" string
        }
      }
    }
    
    enum AppString: String {
      case name = "Name"
      case password = "Password"
    
      var localized: String {
        return NSLocalizedString(self.rawValue, comment: "")
      }
    }
    
    0 讨论(0)
提交回复
热议问题