Manish Agrawal
Manish Agrawal

Reputation: 11026

function can return another function use in swift

Functions are a first-class type. This means that a function can return another function as its value.

func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
    return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)

when i implement this i got the following error:

Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

Also where this functionality can be useful in realtime solutions?

Upvotes: 2

Views: 208

Answers (1)

masoud
masoud

Reputation: 56479

Per documentation, your function declaration should be like below code, See the parentheses around Int. It indicates the entry parameters.

func makeIncrementer() -> (Int) -> Int
                          ^^^^^^^^^^^^

This funcionality can make it easier to have dynamic code based on a value in run-time.

Upvotes: 3

Related Questions