Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide conversion to/from platform specific UUIDs #102

Open
twyatt opened this issue Sep 14, 2020 · 4 comments
Open

Provide conversion to/from platform specific UUIDs #102

twyatt opened this issue Sep 14, 2020 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@twyatt
Copy link
Contributor

twyatt commented Sep 14, 2020

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 twyatt changed the title Provide conversion to platform specific UUIDs Provide conversion to/from platform specific UUIDs Sep 15, 2020
@twyatt
Copy link
Contributor Author

twyatt commented Sep 22, 2020

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.

@rocketraman
Copy link

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.

@benasher44 benasher44 added enhancement New feature or request help wanted Extra attention is needed labels Oct 8, 2020
@Benoye
Copy link

Benoye commented Oct 6, 2021

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

@twyatt
Copy link
Contributor Author

twyatt commented Oct 6, 2021

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?:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants