如何在 Swift 3 中创建具有 QoS 属性的 dispatch_queue?

How do I create a dispatch_queue with QoS attributes in Swift 3?

我在 Swift 2:

中有这样的代码
let attrs = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)
let myQueue = dispatch_queue_create("com.example.serial-queue", attrs)

这不会在 Swift 3 中编译,因为 dispatch_queue_attr_make_with_qos_classdispatch_queue_create 不可用。如何使用自定义 QoS 创建串行队列 class?

DispatchQueue is now a class, and you can use its init(label:attributes:target:) initializer. The attributes are now an OptionSet called DispatchQueueAttributes,它有实例 .serial.qosUtility.

放在一起:

let myQueue = DispatchQueue(label: "com.example.serial-queue",
                            attributes: [.serial, .qosUtility])