Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What () -> () means in swift?

Tags:

swift

extension Int {
    func repetitions(task: () -> ()) {
        for i in 0..<self {
            task()
        }
    }
}

I know that the task is parameter namer. But I do not know what () -> ().

like image 370
jjaeko Avatar asked Aug 31 '14 13:08

jjaeko


People also ask

What means of -> in Swift IOS?

This means that success parameter is a function that receives an object (AnyObject) and returns nothing (Void). Follow this answer to receive notifications. edited Dec 10, 2020 at 18:11.

What is arrow in Swift?

To me, the arrow represents a mapping which I think is really appropriate and natural for functions (or more generally closures) just as it is used in Swift.

What is $0 and $1 in Swift?

$0 and $1 are closure's first and second Shorthand Argument Names (SAN for short) or implicit parameter names, if you like. The shorthand argument names are automatically provided by Swift. The first argument is referenced by $0 , the second argument is referenced by $1 , the third one by $2 , and so on.

What is Autoclosure in Swift?

An autoclosure is a closure that's automatically created to wrap an expression that's being passed as an argument to a function. It doesn't take any arguments, and when it's called, it returns the value of the expression that's wrapped inside of it.


2 Answers

() -> () just means Void -> Void - a closure that accepts no parameters and has no return value.

like image 105
akashivskyy Avatar answered Oct 25 '22 14:10

akashivskyy


More precisely, () -> () means a closure taking a tuple with 0 values as argument and returning a tuple with zero values. Which is equivalent to saying: a closure taking no arguments and with no return value (or returning void)

like image 34
Antonio Avatar answered Oct 25 '22 13:10

Antonio