Swift 3 中 DispatchSource 的 GCD 函数事件处理程序?
GCD function event handler for DispatchSource in Swift 3?
在 Objective-C 中,您可以将调度源的事件处理程序指定为块或函数。来自 Apple 的 Concurrency Programming Guide:
Function-based event handlers take a single context pointer,
containing the dispatch source object, and return no value.
Block-based event handlers take no parameters and have no return
value.
在Swift3中,是否仍然可以使用函数作为事件处理程序?我只看到如何使用块。我需要访问我的处理程序中的源,我需要在定义源的地方单独定义我的处理程序。
这个怎么样?
class Foo {
var eventHandler: (DispatchSourceRead) -> Void
init(handler: @escaping (DispatchSourceRead) -> Void) {
eventHandler = handler
}
}
let foo = Foo() { source in
print("got event from source")
}
let source = DispatchSource.makeReadSource(fileDescriptor: 0)
source.setEventHandler {
foo.eventHandler(source)
}
在 Objective-C 中,您可以将调度源的事件处理程序指定为块或函数。来自 Apple 的 Concurrency Programming Guide:
Function-based event handlers take a single context pointer, containing the dispatch source object, and return no value. Block-based event handlers take no parameters and have no return value.
在Swift3中,是否仍然可以使用函数作为事件处理程序?我只看到如何使用块。我需要访问我的处理程序中的源,我需要在定义源的地方单独定义我的处理程序。
这个怎么样?
class Foo {
var eventHandler: (DispatchSourceRead) -> Void
init(handler: @escaping (DispatchSourceRead) -> Void) {
eventHandler = handler
}
}
let foo = Foo() { source in
print("got event from source")
}
let source = DispatchSource.makeReadSource(fileDescriptor: 0)
source.setEventHandler {
foo.eventHandler(source)
}