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

Pass by-ReferenceSwift

Pass by-Reference Problem Overview


In Swift, structs and value types are passed by value by default, just like in C#. But C# also has a very usable ref keyword, that forces the parameter to be passed by reference, so that the same instance could be changed inside the function and accessed from the caller's scope afterwards. Is there a way to achieve the same result in Swift?

Pass by-Reference Solutions


Solution 1 - Pass by-Reference

Use the inout qualifier for a 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 the docs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax YankovView Question on Stackoverflow
Solution 1 - Pass by-ReferencericksterView Answer on Stackoverflow