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

前端 未结 10 2152
情深已故
情深已故 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:29

    Code for Xcode 8, using the fact that Int works but other methods aren't exposed to Objective-C. This is pretty horrible as it stands...

    class EnumSupport : NSObject {
        class func textFor(logSeverity severity: LogSeverity) -> String {
            return severity.text()
        }
    }
    
    @objc public enum LogSeverity: Int {
        case Debug
        case Info
        case Warn
        case Error
    
        func text() -> String {
            switch self {
                case .Debug: return "debug"
                case .Info: return "info"
                case .Warn: return "warn"
                case .Error: return "error"
            }
        }
    }
    

提交回复
热议问题