UUID

func generateBasedUUID() -> 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 ?? ""
}

results matching ""

    No results matching ""