Jeremias Nunez

    Jeremias Nunez

    1 year ago
    Hi All! I've successfully integrated a KMP module into an existing iOS app, but I'm having an issue with the difference between the generated Obj-C class name (Prefix+ClassName) and the Swift name (ClassName) of one of my classes. When I try to iterate over an array of such classes I'm getting this error:
    Precondition failed: NSArray element failed to match the Swift Array Element type
    Expected Movie but found SharedMovie: file Swift/ArrayBuffer.swift, line 354
    My Movie class is defined like this:
    @Serializable
    data class Movie(
        val name: String,
        val tone: String,
        val light: String,
        val url: String,
        val order: Int,
        val version: Int
    )
    I found this issue but it doesn't provide any possible solution: https://github.com/JetBrains/kotlin-native/issues/2830
    Well, I managed to fix this! In case it helps anyone, the problem was that I was wrapping the array of movies in a response class defined like this:
    sealed class ServiceResponse<out T> {
            class Success<out T>(val data: T) : ServiceResponse<T>()
            data class Error(val exception: Throwable,
                             val code: Int? = null,
                             val error: Boolean? = null,
                             val errors: List<GenericError>? = null,
                             val message: String? = null,
                             val method: String? = null,
                             val path: String? = null) : ServiceResponse<Nothing>()
        }
    Where T was a List<Movie>. The generated Obj-C interface for this becomes ServiceResponse<NSArray>, so when casting between NSArray -> Swift Array, the name difference always caused the cast to fail. What I did was change T to be a class MoviesResponse which has a property that is a list of Movie. In that case the generated Obj-C interface becomes ServiceResponse<MoviesResponse> and accessing the array directly causes no problems