How to make a Swift String enum available in Objective-C?

前端 未结 10 2113
情深已故
情深已故 2020-12-01 07:09

I have this enum with String values, which will be used to tell an API method that logs to a server what kind of serverity a message has. I\'m using Swift 1.2,

10条回答
  •  攒了一身酷
    2020-12-01 07:30

    I think @Remi 's answer crashes in some situations as I had this:

    My error's screesshot. so I post my edition for @Remi 's answer:

    @objc public enum LogSeverity: Int, RawRepresentable {
        case debug
        case info
        case warn
        case error
    
        public typealias RawValue = String
    
        public var rawValue: RawValue {
            switch self {
                case .debug:
                    return "DEBUG"
                case .info:
                    return "INFO"
                case .warn:
                    return "WARN"
                case .error:
                    return "ERROR"
            }
        }
    
        public init?(rawValue: RawValue) {
            switch rawValue {
                case "DEBUG":
                    self = .debug
                case "INFO":
                    self = .info
                case "WARN":
                    self = .warn
                case "ERROR":
                    self = .error
                default:
                    return nil
            }
        }
    }
    

提交回复
热议问题