> For the complete documentation index, see [llms.txt](https://ryukiedev.gitbook.io/wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ryukiedev.gitbook.io/wiki/swift/cow.md).

# 05.Copy-on-write

## 什么是 COW

Copy on write

* COW是一种常见的计算机技术，有助于在复制结构时提高效率
  * 例如一个数组有100个元素，赋值到另一个数组，赋值所有元素，即最终两个数组内容相同
* 当使用COW的话
  * 当将两个变量指向同一数组时，他们指向相同的底层数据。两个变量指向相同的数据可能看起来矛盾，当修改第二个变量的时候，Swift才会去复制一个副本，第一个不会改变。
* 通过延迟复制操作，直到实际使用到的时候 才去复制，以此确保没有浪费的工作。

> COW是特别添加到Swift数组和字典的功能，自定义的数据类型不会自动实现。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/swift/cow.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
