作者名:Florent Vilmart Giordano Scalzo Sergio De Simone
本章字?jǐn)?shù):105字
更新時間:2021-07-02 14:45:05
Value types
First, we need to distinguish value types from reference types. Value types aren't reference counted, as they are values. Think of a simple integer; the value in an integer isn't shared across all of its assignments, but copied whenever it's assigned to a new variable:
struct Box { var intValue: Int }
let box = Box(intValue: 0) var otherBox = box otherBox.intValue = 10 assert(box.intValue == 0) // box and otherBox don't share the same reference assert(otherBox.intValue == 10)
When you're passing a struct around, the value is behaving like it's copied. This is the same behavior seen when capturing values inside blocks.