Swift3 中的 UUID,但“版本 1"样式 UUID
这个问题是关于 Swift.
在 Swift 中生成 rfc UUID 非常容易,得到一个 Swift String
,因为在这个阶段 Apple 已经为它制作了一个 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()
}
在使用 Swift
有没有办法在 Swift3 中做到这一点?(仅限 >9)
Is there a way to do this in Swift3? ( >9 only)
在 Swift 中,如何获取版本 1 UUID.因此,在 UUID()
调用中可能有一些我不知道的选项,或者调用 C 调用并将结果安全地作为 String
调用存在困难>.
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
.
这已经过时了.不要再这样做了.
我会删除答案,但已打勾!
This is incredibly out of date. Don't do this any more.
I'd delete the answer, but it's ticked!
进入 C 调用的 Swift 代码...
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 ?? ""
}