上QQ阅读APP看书,第一时间看更新
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.