【问题标题】:Why is the struct type UUID not sendable?为什么结构类型 UUID 不可发送?
【发布时间】:2021-10-16 14:43:06
【问题描述】:

鉴于 UUID 类型是一个结构,我认为它应该是 Sendable。但是编译器会产生一个警告:

struct S: Sendable {
    var int: Int
    var id: UUID // <-- Stored property 'id' of 'Sendable'-conforming struct 'S' has non-sendable type 'UUID'
}

【问题讨论】:

  • struct 不会自动符合可发送。也许 UUID 对 C code 的引用用于生成唯一标识符是阻止它符合可发送的原因

标签: swift swift-concurrency


【解决方案1】:

你可以这样做:


struct S: Sendable {
    var int: Int
    var id: UUID
}

extension UUID: UnsafeSendable { }

Xcode 告诉我使用@unchecked Sendable 而不是UnsafeSendable,但它还不能识别@unchecked

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 2011-12-06
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2014-10-26
    相关资源
    最近更新 更多