You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently using KMM to iOS there is no constructor for Instant. So creating an instant from epoch milliseconds is impossible from iOS unless creating an additional helper function for this purpose in Kotlin.
jeffdgr8, leandrofavarin, AlexTrotsenko and bartekpacia
This is odd. Our tests include code like Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds()), and they build and run on iOS simulator. Could you please provide of a problematic code snippet and the corresponding error?
But are those tests written in Swift or Objective-c or in Kotlin. In Kotlin it works. It's just that it's not accessible from Swift or Objective-c. Here is the generated obj-c header of Instant:
It should be noted that this can't be changed in kotlinx-datetime. The issue is with the tooling that generates an Apple framework: when you use a particular class in code that is exported to a framework, only its methods are included in the generated header. Companion objects (and, it seems, some extension methods) are not exported.
There are several possible solutions for this:
Don't use kotlinx.datetime in Objective-C/Swift. Instead, use NSDate, NSDateComponents, and other constructs native to the platform and employ converters (see https://github.com/Kotlin/kotlinx-datetime/blob/3af71d2e874592fc70282de441a09d024dcefb54/core/darwin/src/Converters.kt) in the Kotlin code. This is the recommended solution, as using native classes is usually preferable in platform-specific code, given access to more platform features and thus potentially better library support (though for datetime this is arguably less of a factor than for some other libraries).
(A hack) Force the companion object to be included in the framework. For example, upon adding the following code to Platform.kt in iosMain in a sample KMM project, the companion object was included in the headers:
Activity
dkhalanskyjb commentedon Dec 4, 2020
This is odd. Our tests include code like
Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds())
, and they build and run on iOS simulator. Could you please provide of a problematic code snippet and the corresponding error?lammertw commentedon Dec 4, 2020
But are those tests written in Swift or Objective-c or in Kotlin. In Kotlin it works. It's just that it's not accessible from Swift or Objective-c. Here is the generated obj-c header of Instant:
lammertw commentedon Dec 4, 2020
As you can see none of the companion functions are there.
dkhalanskyjb commentedon Dec 8, 2020
Ah, I see.
It should be noted that this can't be changed in
kotlinx-datetime
. The issue is with the tooling that generates an Apple framework: when you use a particular class in code that is exported to a framework, only its methods are included in the generated header. Companion objects (and, it seems, some extension methods) are not exported.There are several possible solutions for this:
NSDate
,NSDateComponents
, and other constructs native to the platform and employ converters (see https://github.com/Kotlin/kotlinx-datetime/blob/3af71d2e874592fc70282de441a09d024dcefb54/core/darwin/src/Converters.kt) in the Kotlin code. This is the recommended solution, as using native classes is usually preferable in platform-specific code, given access to more platform features and thus potentially better library support (though for datetime this is arguably less of a factor than for some other libraries).kotlinx-datetime
to the Apple framework. This could be the best solution for you, but please note that this may increase the compilation time and the size of the resulting binaries.Platform.kt
iniosMain
in a sample KMM project, the companion object was included in the headers:kotlinx-datetime
will benefit automatically.