Swift Playground provided the following code. How's the speakText(graphic: )called without passing in parameters? (Obviously graphic is already placed in another segment of the code)
Swift Playground提供了以下代码。如何在不传递参数的情况下调用speakText(graphic:)? (显然图形已经放在代码的另一部分)
// Speak the text of graphic.
func speakText(graphic: Graphic) {
speak(graphic.text)
}
func addGreeting(touch: Touch) {
if touch.previousPlaceDistance < 60 { return }
let greetings = ["howdy!", "hello", "hi", "ciao", "yo!", "hey!", "what’s up?"]
let greeting = greetings.randomItem
let graphic = Graphic(text: greeting)
graphic.textColor = #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)
graphic.fontName = .chalkduster
scene.place(graphic, at: touch.position)
graphic.rotation = randomDouble(from: -30, to: 30)
}
// Create and add Speak tool.
let speakTool = Tool(name: "Speak", emojiIcon: "????")
speakTool.onGraphicTouched = speakText(graphic: )
scene.tools.append(speakTool)
1 个解决方案
#1
1
speakTool
is of type Tool
which has a property onGraphicTouched
that is of type (Graphic) -> ()
which is a function/closure that takes a Graphic
as input and returns nothing (Void
or ()
).
speakTool是Tool类型,它有一个属性onGraphicTouched,类型为(Graphic) - >(),它是一个函数/闭包,它将一个Graphic作为输入并且不返回任何内容(Void或())。
speakText(graphic:)
is a function pointer to your function defined above. Note that that function has the required signature; it takes a Graphic
and returns nothing.
speakText(graphic :)是指向上面定义的函数的函数指针。请注意,该函数具有所需的签名;它需要一个Graphic并且不返回任何内容。
So speakTool.onGraphicTouched = speakText(graphic: )
assigns a pointer to the function to onGraphicTouched
and when the graphic is touched, the speakTool
will call onGraphicTouched(someGraphic)
which will call speakText(graphic: someGraphic)
.
所以speakTool.onGraphicTouched = speakText(graphic :)将指向函数的指针分配给onGraphicTouched,当触摸图形时,speakTool将调用onGraphicTouched(someGraphic),它将调用speakText(graphic:someGraphic)。
You can read more about this in the section on Function Types in Apple's Swift Guide.
您可以在Apple的Swift Guide中的“函数类型”一节中阅读更多相关内容。
#1
1
speakTool
is of type Tool
which has a property onGraphicTouched
that is of type (Graphic) -> ()
which is a function/closure that takes a Graphic
as input and returns nothing (Void
or ()
).
speakTool是Tool类型,它有一个属性onGraphicTouched,类型为(Graphic) - >(),它是一个函数/闭包,它将一个Graphic作为输入并且不返回任何内容(Void或())。
speakText(graphic:)
is a function pointer to your function defined above. Note that that function has the required signature; it takes a Graphic
and returns nothing.
speakText(graphic :)是指向上面定义的函数的函数指针。请注意,该函数具有所需的签名;它需要一个Graphic并且不返回任何内容。
So speakTool.onGraphicTouched = speakText(graphic: )
assigns a pointer to the function to onGraphicTouched
and when the graphic is touched, the speakTool
will call onGraphicTouched(someGraphic)
which will call speakText(graphic: someGraphic)
.
所以speakTool.onGraphicTouched = speakText(graphic :)将指向函数的指针分配给onGraphicTouched,当触摸图形时,speakTool将调用onGraphicTouched(someGraphic),它将调用speakText(graphic:someGraphic)。
You can read more about this in the section on Function Types in Apple's Swift Guide.
您可以在Apple的Swift Guide中的“函数类型”一节中阅读更多相关内容。