Is there a way to determine if a variable passed in is reference type or value type?

后端 未结 2 1316
走了就别回头了
走了就别回头了 2021-01-03 08:54

In Swift, is there a way to determine through code if a variable passed in is reference type or value type?

For example, is tuple a value type or reference type?

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 09:40

    afaik, to pass by reference you need to add the inout keyword in front of the parameter definition. all other parameters are constants unless prefixed with the var keyword

    “Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake.”

    and...

    “Variable parameters, as described above, can only be changed within the function itself. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.”

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

提交回复
热议问题