Fattie
Fattie

Reputation: 12582

UUID in Swift3, but "version 1" style UUID

This question is about Swift.

It's very easy to generate a rfc UUID in Swift getting a Swift String as at this stage Apple have made a Swift method for it...

func sfUUID()->String
    {
    return UUID().uuidString.lowercased()
    }

I need an old-style "version 1" UUID, when using Swift

(Example: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_.28date-time_and_MAC_address.29)

Is there a way to do this in Swift3? ( >9 only)

In Swift, how to get a Version 1 UUID. So, there might be some option I don't know about on the UUID() call, or there's the difficulty of calling a C call and getting the result safely as a String.

Upvotes: 3

Views: 1978

Answers (2)

Fattie
Fattie

Reputation: 12582

This is incredibly out of date. Don't do this any more.

I'd delete the answer, but it's ticked!

Swift code which gets to the C call...

func generateVersionOneAkaTimeBasedUUID() -> String {

    // figure out the sizes

    let uuidSize = MemoryLayout<uuid_t>.size
    let uuidStringSize = MemoryLayout<uuid_string_t>.size

    // get some ram

    let uuidPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: uuidSize)
    let uuidStringPointer = UnsafeMutablePointer<Int8>.allocate(capacity: uuidStringSize)

    // do the work in C

    uuid_generate_time(uuidPointer)
    uuid_unparse(uuidPointer, uuidStringPointer)

    // make a Swift string while we still have the C stuff

    let uuidString = NSString(utf8String: uuidStringPointer) as? String

    // avoid leaks

    uuidPointer.deallocate(capacity: uuidSize)
    uuidStringPointer.deallocate(capacity: uuidStringSize)

    assert(uuidString != nil, "uuid (V1 style) failed")
    return uuidString ?? ""
}

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299275

Callam's link is the actual answer. Swift can (and in this case must) call C, so the name of the C function is all you really need.

But the "Swift" question here is just how to call a C function from Swift, which is a general skill that's more important than this particular question (and isn't really well explained in the current documentation). So it's worth studying how the following works, and not taking it as just the answer to "how do you generate a v1 UUID in Swift."

var uuid: uuid_t = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
withUnsafeMutablePointer(to: &uuid) {
    $0.withMemoryRebound(to: UInt8.self, capacity: 16) {
        uuid_generate_time($0)
    }
}

let finalUUID = UUID(uuid: uuid)

Upvotes: 6

Related Questions