Skip to content

Type Mapping

swift4j maps Swift types to their JVM equivalents automatically. The tables below show the mapping for each supported category.

SwiftKotlinJava
BoolBooleanboolean
IntLonglong
Int32Intint
Int16Shortshort
Int8Bytebyte
UInt8Bytebyte
FloatFloatfloat
DoubleDoubledouble

Int maps to Long because Swift’s Int is 64-bit on all platforms swift4j targets.

SwiftKotlinJava
StringStringString
SwiftKotlinJava
[Int]LongArraylong[]
[Double]DoubleArraydouble[]
[Float]FloatArrayfloat[]
[Bool]BooleanArrayboolean[]
[String]Array<String>String[]
[T] where T is @jvmArray<T>T[]
SwiftKotlinJava
T? where T is a classT?@Nullable T
Int?Long?Long (boxed)
String?String?@Nullable String
(() -> Void)?(() -> Unit)?@Nullable Runnable
SwiftKotlinJava
() -> Void() -> UnitRunnable
(T) -> Void(T) -> UnitConsumer<T>
(T) -> R(T) -> RFunction<T, R>
(T, U) -> R(T, U) -> RBiFunction<T, U, R>

All types in the closure signature must be mapped types or @jvm-annotated types.

SwiftKotlin / Java
async func f() -> TCompletableFuture<T>
async func f() throws -> TCompletableFuture<T> (exception in future)

Classes, structs, and enums annotated with @jvm map to their generated proxy class by name. The package is the target name (hyphens replaced with underscores).

SwiftKotlin
@jvm class FooMyTarget.Foo
@jvm struct BarMyTarget.Bar
@jvm enum Baz (no assoc. values)MyTarget.Baz (enum)
@jvm enum Baz (with assoc. values)MyTarget.Baz (sealed class)

Types not listed above cannot be exported. Attempting to use them in an exported method signature causes a compiler warning from @jvm. Use @nonjvm to suppress the warning for members that intentionally use unexportable types.