Is Swift Pass By Value or Pass By Reference

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

    Classes are passed by references and others are passed by value in default. You can pass by reference by using the inout keyword.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:18

    It is always pass-by-value when the parameter is not inout.

    It is always pass-by-reference if the parameter is inout. However, this is somewhat complicated by the fact you need to explicitly use the & operator on the argument when passing to an inout parameter, so it may not fit the traditional definition of pass-by-reference, where you pass the variable directly.

    0 讨论(0)
  • 2020-11-22 10:19

    When you use inout with an infix operator such as += then the &address symbol can be ignored. I guess the compiler assumes pass by reference?

    extension Dictionary {
        static func += (left: inout Dictionary, right: Dictionary) {
            for (key, value) in right {
                left[key] = value
            }
        }
    }
    

    origDictionary += newDictionaryToAdd

    And nicely this dictionary 'add' only does one write to the original reference too, so great for locking!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:23

    Everything in Swift is passed by "copy" by default, so when you pass a value-type you get a copy of the value, and when you pass a reference type you get a copy of the reference, with all that that implies. (That is, the copy of the reference still points to the same instance as the original reference.)

    I use scare quotes around the "copy" above because Swift does a lot of optimization; wherever possible, it doesn't copy until there's a mutation or the possibility of mutation. Since parameters are immutable by default, this means that most of the time no copy actually happens.

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