Hey! I’m currently learning to write Kotlin/Native...
# kotlin-native
p
Hey! I’m currently learning to write Kotlin/Native code that uses Apple framework packages. Most of the cases are clear, but this particular problem troubles me. How to write this piece of code, especially the
extension
part in Kotlin.
Copy code
lass LocationService: NSObject {
    
    let locationManager: CLLocationManager
    init(locationManager: CLLocationManager = CLLocationManager()) {
    	self.locationManager = locationManager
    	super.init()
    	self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    	self.locationManager.delegate = self
    }
    func getCurrentLocation() {
    	self.locationManager.requestLocation()
    	self.locationManager.requestWhenInUseAuthorization()
    }
}

extension LocationService: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
    	guard let location = locations.first else { return }
    	print("The location is: \(location)")
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    	print("Error: \(error)")
    }
}
I do know that the original
CLLocationManagerDelegate
protocol is actually interface with a name
CLLocationManagerDelegateProtocol
on the Kotlin/Native side. I tried googling, but did not find any examples on how to approach Swift extensions in Kotlin/Native. Any help would be highly appreciated, thanks!
To give a bit more context, the example is taken from here: https://apiumhub.com/tech-blog-barcelona/improving-testability-cllocationmanager/
g
I'm not really sure, but is it even possible to use extensions from objc? I think it's Swift only function, and because Kotlin interop is based on objc, there is no way to use it too, only by exposing this extension from Swift using @objc annotation But I may be wrong, sorry, it's just my understanding how objc/swift interop works
p
Yeah, Iwas a bit afraid of that — I think you might be right. In that case I would also be interested in hearing on how to implement the delegate part in Kotlin, so that it fullfills the type required for the delegate (one that implements CLLocationManagerDelegate). I’m a bit lost here.
g
in general it should be possible if it’s possible to do from ObjC
s
In Kotlin you can’t implement protocol with an extension. This is not related to Objective-C, which has this feature just as Swift does. Please implement
CLLocationManagerDelegateProtocol
directly with
LocationService
.