Currying is supported in Swift. It means we could transform a multiple parameters method into a new method, to make it flexible. Swift is more or less a functional programming language. Currying is an important bridge between normal and functional way of thinking.

To make it clearer, we could write a method to add 1 to an input number in Swift, like this:

func addOne(num: Int) -> Int {
    return num + 1
}

It could only express a very simple operation. If then we need to add 2 or 3 to the number, we might need to create a new version of similar function by returning num + 2 or num + 3 respectively. Is there a better way for it? We could define a function which accepts the add number (like 1, 2 or 3 in this example) we need, then returns another function to add the two numbers.

func addTo(adder: Int) -> Int -> Int {
    return {
        num in
        return num + adder
    }
}

Once we have addTo, we could produce functions like addOne or addTwo easily:

let addTwo = addTo(2)    // addTwo: Int -> Int
let result = addTwo(6)   // result = 8

Another example, we could write a greaterThan method in similar way:

func greaterThan(comparer: Int) -> Int -> Bool {
    return { $0 > comparer }
}

let greaterThan10 = greaterThan(10);

greaterThan10(13)    // => true
greaterThan10(9)     // => false

Currying is an effective way to mass-produce similar methods. We could avoid boilerplate by currying a method template. It also helps us to maintain the code with less pain.

A real use case: we mentioned that in Swift a Selector could only be created with a string. This causes a serious problem when we want to do some refactoring. There would be no compiler check for us to rely on. It is really dangerous to do this without the help of the compiler. Target-action pattern is so widely used in Cocoa development. So how can we use the selector in a safer way?

Yes, one possible solution is using Currying. Ole Begemann gave us a great implementation about it here. It uses a wrapper and makes the action curried to supply additional safety for target-action pattern in Swift.

protocol TargetAction {
    func performAction()
}

struct TargetActionWrapper<T: AnyObject>: 
                            TargetAction {
    weak var target: T?
    let action: (T) -> () -> ()

    func performAction() -> () {
        if let t = target {
            action(t)()
        }
    }
}

enum ControlEvent {
    case TouchUpInside
    case ValueChanged
    // ...
}


class Control {
    var actions = [ControlEvent: TargetAction]()

    func setTarget<T: AnyObject>(target: T, 
                   action: (T) -> () -> (), 
                controlEvent: ControlEvent) {

        actions[controlEvent] = TargetActionWrapper(
            target: target, action: action)
    }

    func removeTargetForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent] = nil
    }

    func performActionForControlEvent(controlEvent: ControlEvent) {
        actions[controlEvent]?.performAction()
    }
}