Discussion 解説
When declaring a function or method, you don’t need to specify a return type if no value will be returned. However, the type of a function, method, or closure always includes a return type, which is Void
if otherwise unspecified.
関数またはメソッドを宣言するとき、値が返されることがないならばあなたは戻り型を指定する必要はありません。しかしながら、関数、メソッド、またはクロージャは、常に戻り型を含みます、それは指定されないならばVoid
です。
Use Void
or an empty tuple as the return type when declaring a closure, function, or method that doesn’t return a value.
値を返さないクロージャ、関数、またはメソッドを定義する場合は、Void
または空のタプルを戻り型として使ってください。
// No return type declared:
func logMessage(_ s: String) {
print("Message: \(s)")
}
let logger: (String) -> Void = logMessage
logger("This is a void function")
// Prints "Message: This is a void function"