Hello everyone! I'm getting the following error wh...
# multiplatform
j
Hello everyone! I'm getting the following error when building the native library:
kotlin super calls to objective-c protocols are not allowed
. Has anyone encountered this/knows how to solve it? I'll add more details in the thread
I have the following code, to obtain users'l locations:
Copy code
internal class Tracker(
    private val onNewLocation: (Location) -> Unit,
) : NSObject(), CLLocationManagerDelegateProtocol {
    override fun locationManager(manager: CLLocationManager, didUpdateLocations: List<*>) {
        delegate.locationManager(manager, didUpdateLocations)
        (didUpdateLocations.last() as? CLLocation)?.let { location ->
            location.coordinate().useContents {
                onNewLocation(Location(latitude, longitude))
            }
        }
    }
}
If I don't extend from NSObject, it tells me I have to, so that does not seem to be an option. I've found a workaround that compiles, but it has 2 issues: • It requires boilerplate • It gives me several warnings I do not understand, and I don't know its impact The solution is to receive an instance of a class and delegate to it, instead of extending from NSObject: ` ``
Copy code
internal class Tracker(
    private val onNewLocation: (Location) -> Unit,
    private val delegate: LocationManagerDelegate = LocationManagerDelegate(),
) : CLLocationManagerDelegateProtocol by delegate {
    override fun locationManager(manager: CLLocationManager, didUpdateLocations: List<*>) {
        delegate.locationManager(manager, didUpdateLocations)
        (didUpdateLocations.last() as? CLLocation)?.let { location ->
            location.coordinate().useContents {
                onNewLocation(Location(latitude, longitude))
            }
        }
    }
}

class LocationManagerDelegate(): NSObject(), CLLocationManagerDelegateProtocol
With this solution, I get the wasning
Delegated member 'fun locationManager(manager: CLLocationManager, didEnterRegion: CLRegion): Unit' hides supertype override: public open expect fun locationManager(manager: CLLocationManager, didEnterRegion: CLRegion): Unit defined in platform.CoreLocation.CLLocationManagerDelegateProtocol. Please specify proper override explicitly
for each of the methods that are being delegated.
So in summary: • Is there a better solution? • And what's the issue with hiding the supertype override? using delegates is a common pattern in Kotlin