如何编写一个返回另一个带有 in-out 参数的函数的函数?
我想编写makeIncrementor
返回函数的incrementor
函数。此incrementor
函数采用一个 In-Out 参数并将其递增一定量(它不返回任何内容)。这是我的代码:
func makeIncrementor(amount:Int) -> Int->Void {
func incrementor(inout variable:Int) -> Void {
variable += amount;
}
return incrementor;
}
var x = 1;
var inc = makeIncrementor(2);
inc(&x)
//x should now contain 3
但是,Xcode 给出以下错误:
<REPL>:9:12: error: 'Int' is not a subtype of 'inout Int'
return incrementor;
^
我究竟做错了什么?