uuid icon indicating copy to clipboard operation
uuid copied to clipboard

Provide conversion to/from platform specific UUIDs

Open twyatt opened this issue 4 years ago • 4 comments

Libraries that have Uuid (provided by this library) on their public API, may need to internally call native APIs that expect or return native UUID representations (e.g. NSUUID on iOS).

It would be helpful to provide platform specific conversions from Uuid to native UUID types (e.g. fun Uuid.toNSUUID(): NSUUID), and visa versa.

twyatt avatar Sep 14 '20 20:09 twyatt

Conversion ended up being simpler than I had expected, with the caveat that I'm not sure of the performance implications of using Strings as the conversion mechanism. There are probably more performant means of conversion, but the following approaches are sufficing for me thus far:

JVM

fun Uuid.toUUID(): UUID = UUID.fromString(toString())
fun UUID.toUuid(): Uuid = uuidFrom(toString())

Apple

fun Uuid.toNSUUID(): NSUUID = NSUUID(toString())
fun NSUUID.toUuid(): Uuid = uuidFrom(UUIDString)

Javascript

As far as I can tell, Javascript doesn't have a standard UUID type (they're often taken as plain Strings), so I've been using the provided uuidFrom and toString (on Uuid type) directly.

twyatt avatar Sep 22 '20 16:09 twyatt

To get at least part of the way towards a more efficient byte-based conversion from NSUUID see https://stackoverflow.com/questions/41597877/get-data-from-uuid-in-swift-3.

rocketraman avatar Oct 08 '20 14:10 rocketraman

Hi @twyatt ! I just saw your comment and I would like to do the same thing.

I am new to KMM, could you help me ?

If I want to use the functions you defined, am I supposed to put them in the kotlin iOSMain and call them from my Swift code in XCode ? I am trying to generate an uuid in the swift code.. so far these functions are not seen by XCode in my project

Benoye avatar Oct 06 '21 09:10 Benoye

I use them internally in the Kable library. They are in the appleMain sourceset (they could also live in an iosMain sourceset as you mentioned).

Unfortunately I haven't worked much with Swift calling into Kotlin code, so I probably can't help you there much.

There is interop documentation that may prove helpful?:

twyatt avatar Oct 06 '21 09:10 twyatt