Search code examples
iosswiftkotlinswiftuikotlin-multiplatform

Cannot convert value of type 'KotlinBoolean' to expected argument type 'Bool'


Im making a Multiplatform KMM project, and im having trouble assigning dynamic value to the button property.

Basically I have this like button which changes its states depending on selected property:

struct SelectFavouriteButton: View {
    
    @State var selected = false    
    let action: (Bool) -> Void
      
    var body: some View {
        Button(action: {
            selected.toggle()
            self.action(selected)
        }) {
            Image(systemName: selected ? "heart.fill" : "heart" )
                .foregroundColor(.accentRed)
        }
    }
}

The view model from Kotlin part:

@Serializable
data class Data(
...
    var isLiked: Boolean? = null,
    var isSaved: Boolean? = null,
...
}

And the view where this button is called:

struct CellView: View {
    
    @State var advert: Data?

    var body: some View {

     // Cannot convert value of type 'KotlinBoolean' to expected argument type 'Bool'
     // Insert ' as! Bool'
     SelectFavouriteButton(selected: advert?.isLiked ?? false){ isPressed in   // Error is here
         self.action(isPressed, advert?.id ?? "" , false)   
     }

   }
}

Should I use mapping? If yes how do I do it in the current context? Pls help


Solution

  • Kotlin primitives transferred to NSNumber in swift, so try the following (not tested - idea only)

     let linked = advert.isLiked as? NSNumber // or NSNumber?
     SelectFavouriteButton(selected: linked?.boolValue ?? false){ isPressed in
         self.action(isPressed, advert?.id ?? "" , false)   
     }