Open lammertw opened 3 years ago
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:
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("Kotlinx_datetimeInstant")))
@interface CommonKotlinx_datetimeInstant : CommonBase <CommonKotlinComparable>
- (int32_t)compareToOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("compareTo(other:)")));
- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)")));
- (NSUInteger)hash __attribute__((swift_name("hash()")));
- (CommonKotlinx_datetimeInstant *)minusDuration:(double)duration __attribute__((swift_name("minus(duration:)")));
- (double)minusOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("minus(other:)")));
- (CommonKotlinx_datetimeInstant *)plusDuration:(double)duration __attribute__((swift_name("plus(duration:)")));
- (int64_t)toEpochMilliseconds __attribute__((swift_name("toEpochMilliseconds()")));
- (NSString *)description __attribute__((swift_name("description()")));
@property (readonly) int64_t epochSeconds __attribute__((swift_name("epochSeconds")));
@property (readonly) int32_t nanosecondsOfSecond __attribute__((swift_name("nanosecondsOfSecond")));
@end;
@interface CommonKotlinx_datetimeInstant (Extensions)
@property (readonly) CommonKotlinx_datetimeLocalDate *localDate __attribute__((swift_name("localDate")));
@end;
As you can see none of the companion functions are there.
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
in iosMain
in a sample KMM project, the companion object was included in the headers:
fun Instant.Companion.dummy(): Nothing = TODO()
kotlinx-datetime
will benefit automatically.
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.