https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Adam McDonald

03/26/2024, 1:39 PM
Hello 👋 Has anyone had experience creating extension functions for Floats in
common
which then translate to appropriate usages for Swift/iOS? For example:
Copy code
data class Measurement(val type:String, val value:Float)

fun Float.metres() = Measurement("Metre", this)
What I currently end up with being generated on the iOS side in the header file is given the extension function lives in the file `FloatExt.kt`:
Copy code
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("FloatExtKt")))
@interface FloatExtKt : KotlinBase
+ (Measurement *)metres:(float)receiver __attribute__((swift_name("metres(_:)")));
@end
Which suggests that it’s not an extensions on the iOS “float” type but accessible via
FloatExtKt.metres()
🤔
m

Michael Krussel

03/26/2024, 1:44 PM
Extensions on Objective-C classes are not supported in the interop, so you end up with this.
t

tylerwilson

03/26/2024, 1:57 PM
For these kind of things, I always have a Swift file with Swift extensions that wrap these to be used nicely anywhere else in the project.
5 Views