Neal Sanche

    Neal Sanche

    1 year ago
    Hi all, I've been trying to call a method that takes a nullable Int in Kotlin common code from Swift. The method signature is:
    suspend fun getEpisodes(page: Int?): EpisodePage
    On the Swift side, have been try to write code but always am told:
    Cannot convert value of type 'Int32' to expected argument type 'KotlinInt?'
    This confuses me, and I'm sure there's a simple explanation. I can change the function signature to
    getEpisodes(page: Int = 1)
    instead, and that's much easier to deal with. So I'm curious what's going on here?
    I've been thinking it's doing something to box up the Int? because native ints can't be null. I guess I'd love to see a nice example of how to work with this on the Swift side.
    b

    Barco

    1 year ago
    It needs to be cast to NSNumber
    There is a link to an example there
    Neal Sanche

    Neal Sanche

    1 year ago
    My Swift is not so good, admittedly, but this is not working for me.
    b

    Barco

    1 year ago
    What does the suggested fix do?
    Neal Sanche

    Neal Sanche

    1 year ago
    Crashes with
    Could not cast value of type '__NSCFNumber' (0x7fff86d65440) to 'SharedInt' (0x10df11200).
    Okay, getting closer here.
    class EpisodeViewModel: ObservableObject {
        @Published public var episodes: [Episode] = []
        let repository = TestRepository()
        var nextPage: NSNumber? = nil
        
        func fetchEpisodes() {
            repository.getEpisodes(page: (self.nextPage as? KotlinInt)) { (data, error) in
                if let newEpisodes = data?.episodes {
                    self.episodes.append(contentsOf: newEpisodes)
                }
                self.nextPage = data?.nextPage
            }
        }
        
        public var shouldDisplayNextPage: Bool {
            return nextPage != nil
        }
    }
    b

    Barco

    1 year ago
    How about just using the constructor of KotlinInt?
    KotlinInt(self.nextPage!)
    Neal Sanche

    Neal Sanche

    1 year ago
    The above code works now. I guess it's just my own learning how things are being mapped. Thanks for the help though, @Barco
    b

    Barco

    1 year ago
    I think we both learned something 🙂