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

前端 未结 1 1425
盖世英雄少女心
盖世英雄少女心 2020-12-08 04:05

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 refer

相关标签:
1条回答
  • 2020-12-08 04:18

    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.

    0 讨论(0)
提交回复
热议问题