Does Swift have something like the "ref" keyword that causes a parameter to be passed by reference?

In Swift, structures and value types are passed by default, as in C #. But C # also has a very useful ref keyword, which forces the parameter to be passed by reference, so that the same instance can be changed inside the function and then accessible from the call area. Is there a way to achieve the same result in Swift?

+43
pass-by-reference swift
Jun 02 '14 at 22:05
source share
1 answer

Use the inout for the function parameter.

 func swapTwoInts(a: inout Int, b: inout Int) { let temporaryA = a a = b b = temporaryA } swapTwoInts(&someInt, &anotherInt) 

See Function Parameters and Return Values in Documents.

+71
Jun 02 '14 at 22:08
source share



All Articles