in-out パラメーターを受け取る別の関数を返す関数を作成するにはどうすればよいですか?
makeIncrementor
関数を返す関数を書きたいincrementor
。このincrementor
関数は、1 つの 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;
^
私は何を間違っていますか?