官术网_书友最值得收藏!

  • Hands-On Design Patterns with Swift
  • Florent Vilmart Giordano Scalzo Sergio De Simone
  • 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.

主站蜘蛛池模板: 济南市| 桐梓县| 鲁山县| 北辰区| 莒南县| 宿松县| 开平市| 游戏| 伊川县| 新蔡县| 栾城县| 小金县| 苍南县| 毕节市| 定结县| 香港 | 道孚县| 永胜县| 广河县| 曲水县| 乌拉特中旗| 永仁县| 顺平县| 涿鹿县| 清河县| 浦城县| 景东| 鄱阳县| 惠水县| 河北省| 镇江市| 科技| 三门县| 镇康县| 江川县| 曲松县| 普兰店市| 永德县| 巴楚县| 靖边县| 伽师县|