Hi all, I've been trying to call a method that tak...
# multiplatform
n
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:
Copy code
suspend fun getEpisodes(page: Int?): EpisodePage
On the Swift side, have been try to write code but always am told:
Copy code
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
It needs to be cast to NSNumber
There is a link to an example there
n
My Swift is not so good, admittedly, but this is not working for me.
b
What does the suggested fix do?
n
Crashes with
Copy code
Could not cast value of type '__NSCFNumber' (0x7fff86d65440) to 'SharedInt' (0x10df11200).
Okay, getting closer here.
Copy code
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
How about just using the constructor of KotlinInt?
KotlinInt(self.nextPage!)
n
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
I think we both learned something 🙂