它是一个带有闭包的属性(一个函数,或者更好:一个代码块,CGContext
在这种情况下将 a 作为参数)。它什么也不做。它忽略了CGContext
(这就是_ in
部分)。
稍后在示例中有此功能:
public func showCoreGraphicsDiagram(title: String, draw: (CGContext)->()) {
let diagramView = CoreGraphicsDiagramView(frame: drawingArea)
diagramView.draw = draw
diagramView.setNeedsDisplay()
XCPlaygroundPage.currentPage.liveView = diagramView
}
您可以在其中提供另一个闭包(CGContext) -> ()
,然后将此新闭包分配给该draw
属性。
在drawRect
函数中它被调用:draw(context)
.
所以,基本上你可以提供一个绘制一些东西的代码块,例如
showCoreGraphicsDiagram("Diagram Title", draw: { context in
// draw something using 'context'
})
甚至更短的“尾随闭包语法”:
showCoreGraphicsDiagram("Diagram Title") { context in
// draw something using 'context'
}