Is Swift Pass By Value or Pass By Reference

后端 未结 9 611
[愿得一人]
[愿得一人] 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:19

    Classes and structures

    One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.

    Closures

    If you assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its members, you will create a strong reference cycle between the closure and the instance. Swift uses capture lists to break these strong reference cycles

    ARC(Automatic Reference Counting)

    Reference counting applies only to instances of classes. Structures and enumerations are value types, not reference types, and are not stored and passed by reference.

提交回复
热议问题