Is Swift Pass By Value or Pass By Reference

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

    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!

提交回复
热议问题