Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
preface
Since today, I have participated in the programmer’s essential knowledge activity. Considering the duration and length of the activity, I plan to explain only one simple knowledge point in each article.
Because did not do too much preparation, the possible content will be more mixed you forgive me.
So let’s get started.
The most familiar stranger Void
If you had to think back to when you were writing Swift and you used Void, it would probably be when you were writing closures, let’s look at a system API:
open func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
Copy the code
Let’s look at the type of the completion parameter — (() -> Void)? , we remove the optional type of interference and remove the outermost () and? So you get () -> Void.
When we define a closure with no arguments and no return value, we might write:
var callback: (() -> Void)?
Copy the code
Here’s another way to write it:
var callback: (() -> ())?
Copy the code
You’ll see that these are equivalent!
Why is that? Because this is what the source definition says:
/// The return type of functions that don't explicitly specify a return type,
/// that is, an empty tuple `()`.
///
/// 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.
/// Use `Void` or an empty tuple as the return type when declaring a closure,function, or method that doesn't return a value.
///
/// // 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"
public typealias Void = ()
Copy the code
Let’s take a closer look at this comment and example:
A function that does not explicitly specify its return type is an empty tuple '()'. When declaring a function (method), you do not have to specify the return type if it does not return a value. Note, however, that a function (method) or closure always has a return type, Void if not specified. Use Void or empty tuples to define a function (method) or closure that has no return value.Copy the code
This function:
func logMessage(_ s: String) {
print("Message: \(s)")
}
Copy the code
It can be written in the following two forms:
func logMessage1(_ s: String) -> Void {
print("Message: \(s)")
}
func logMessage2(_ s: String) -> () {
print("Message: \(s)")
}
Copy the code
Even with return () there is no problem, the compiler will not report an error
func logMessage3(_ s: String) -> Void {
print("Message: \(s)")
return ()
}
func logMessage4(_ s: String) -> () {
print("Message: \(s)")
return ()
}
Copy the code
And its type is known from this code, which is an explicit declaration of a function with a variable, so it’s (String) -> Void.
let logger: (String) -> Void = logMessage
Copy the code
Pay attention to
When defining a closure, I prefer to use Void instead of () if the return value is null, because it is more comfortable and easier to understand.
❤ var callback: (() -> Void)?
🙅🏻♀️ var callback: (() -> ())?
conclusion
Well, that’s all for today.
Void is nothing special. It represents an empty tuple, just like Int, String, or String.
We’ll see you next time.