Enums
swift4j exports Swift enums in two ways depending on whether the cases have associated values.
Simple enums
Section titled “Simple enums”Enums with no associated values map to Java/Kotlin enums:
import Swift4j
@jvmenum Priority { case low case medium case high}val p: Priority = Priority.highprintln(p) // highPass enum values to and from exported functions normally:
@jvmclass Task { static func label(for priority: Priority) -> String { switch priority { case .low: return "Low" case .medium: return "Medium" case .high: return "High" } }}println(Task.label(for: Priority.medium)) // MediumEnums with associated values
Section titled “Enums with associated values”Enums with associated values map to Kotlin sealed classes. Each case becomes a subclass of the sealed class, with the associated values as properties:
@jvmenum NetworkResult { case success(String) case error(code: Int, message: String) case loading}The generated Kotlin looks like:
sealed class NetworkResult { class success(val value0: String) : NetworkResult() class error(val code: Int, val message: String) : NetworkResult() object loading : NetworkResult()}Use Kotlin when to pattern-match:
val result = Network.requestError()
val msg = when (result) { is NetworkResult.success -> "OK: ${(result as NetworkResult.success).value0}" is NetworkResult.error -> "Error ${result.code}: ${result.message}" NetworkResult.loading -> "Loading..."}println(msg)Cases without associated values become object singletons. Cases with a single unnamed associated value use value0, value1, etc. Cases with named associated values use those names directly.
Passing enum values across the boundary
Section titled “Passing enum values across the boundary”Enum values can be passed as parameters and return values just like any other exported type:
@jvmstruct Network { static func forward(_ result: NetworkResult) -> NetworkResult { switch result { case .success(let s): return .success("Forwarded: \(s)") case .error(let i, let s): return .error(code: -i, message: s) case .loading: return .loading } }}val original = NetworkResult.success("data")val forwarded = Network.forward(original)