EnumType.swift 755 B

12345678910111213141516171819202122232425262728293031
  1. //
  2. // EnumType.swift
  3. // HandyJSON
  4. //
  5. // Created by zhouzhuo on 16/07/2017.
  6. // Copyright © 2017 aliyun. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol _RawEnumProtocol: _Transformable {
  10. static func _transform(from object: Any) -> Self?
  11. func _plainValue() -> Any?
  12. }
  13. extension RawRepresentable where Self: _RawEnumProtocol {
  14. public static func _transform(from object: Any) -> Self? {
  15. if let transformableType = RawValue.self as? _Transformable.Type {
  16. if let typedValue = transformableType.transform(from: object) {
  17. return Self(rawValue: typedValue as! RawValue)
  18. }
  19. }
  20. return nil
  21. }
  22. public func _plainValue() -> Any? {
  23. return self.rawValue
  24. }
  25. }