func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
above is a simple example code for Function as first-class type in Swift now, when i call the call the function in the following way:
var increment = makeIncrementer()
increment(7)
it perfectly gives the answer
But out of curiosity i tried the direct approach ie
makeIncrementer(7) // error
and it gives an error
why is it so??? PS I am a beginner in Swift
The call makeIncrementer()
returns the function, so to call it you pass the parameter in a second set of parentheses:
makeIncrementer()(7)
The error message is given because Swift interprets makeIncrementer(7)
as 7
being passed to makeIncrementer
which doesn't take any parameters. Hopefully Swift error messages are made more friendly in the future. While technically correct, the error message given leads to a lot of confusion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.