3

In my Swift native module for react-native I am trying to export to javascript a Swift enum:

@objc(InterfaceOrientationManager)
class InterfaceOrientationManager: NSObject {

  enum InterfaceOrientation: String {
    case landscapeRight
    case landscapeLeft
    case portrait
    case portraitUpsideDown
  }

  @objc
  static func requiresMainQueueSetup() -> Bool {
    return true
  }

  @objc
  func constantsToExport() -> [String: Any]! {
    return [
      "InterfaceOrientation": InterfaceOrientation
    ]
  }

However this isn't working I get the error "Expected member name or constructor call after type name". A screenshot is at bottom.

Is there any way to accomplish this? I was hoping to avoid doing a dictionary:

 let InterfaceOrientation: [String: String] = [
    "landscapeRight": "landscapeRight",
    "landscapeLeft": "landscapeLeft",
    "portrait": "portrait",
    "portraitUpsideDown": "portraitUpsideDown"
  ]

enter image description here

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    Compare https://stackoverflow.com/a/30480399/1187415: “@objc enums must declare an **integer raw type**” – Martin R May 02 '19 at 17:14
  • 1
    And you probably want an enum *value* in the dictionary (e.g. InterfaceOrientation.landscapeRight), not the enum *type.* – Martin R May 02 '19 at 17:16
  • Thanks @MartinR - so it seems the only way to export an enum with values as strings is to make it a dict? – Noitidart May 02 '19 at 17:29

1 Answers1

0

Create a dictionary from enum like this:

enum InterfaceOrientation: CaseIterable {
    case landscapeRight, landscapeLeft, portrait, portraitUpsideDown
}

var interfaceDictionary: [InterfaceOrientation: String] = [:]
for orientation in InterfaceOrientation.allCases {
    interfaceDictionary[orientation] = "\(orientation)"
}
Jan12
  • 101
  • 7