Is Swift Pass By Value or Pass By Reference

后端 未结 9 716
[愿得一人]
[愿得一人] 2020-11-22 10:04

I\'m really new to Swift and I just read that classes are passed by reference and arrays/strings etc. are copied.

Is the pass by reference the same way as in Objecti

9条回答
  •  春和景丽
    2020-11-22 10:16

    The Apple Swift Developer blog has a post called Value and Reference Types that provides a clear and detailed discussion on this very topic.

    To quote:

    Types in Swift fall into one of two categories: first, “value types”, where each instance keeps a unique copy of its data, usually defined as a struct, enum, or tuple. The second, “reference types”, where instances share a single copy of the data, and the type is usually defined as a class.

    The Swift blog post continues to explain the differences with examples and suggests when you would use one over the other.

提交回复
热议问题