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

# Swift

Swift uses purely ARC. How does it fix the problems that arise without a tracing garbage collector?

## Fixing Reference Cycles

Swift has two keywords that help fix reference cycles when they are found. The first is `weak`, which does not increment the reference count and instead becomes `nil` whenever the instance being referred to is deallocated. `weak` references must therefore be nullable.

The second keyword is `unowned`, which like `weak` creates no strong reference but also cannot be nullable. If the object being referred to is deallocated, the app will crash when the unowned reference is used.

In summary, Swift leaves it up to the developer to identify and fix reference cycles.

## Fixing Races caused by Multi-Threading

Swift requires synchronized access to any object that multiple threads can access concurrently. By this we mean that any **conflicting** operations are **ordered** and executed one after the other, rather than at once. It does this through the use of [Actors](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/#Actors), classes which serialize access to prevent data races, and [`Sendable`](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/#Sendable-Types), a type trait that lets Swift know that a type is safe to send between threads (more accurately, **concurrency domains**).

Note that Swift does not **restrict** invalid access, but it classifies it as **undefined** if you do not follow its concurrency model. For example, the following compiles:

```swift
class Box {
    var value = 0
}

let box = Box()

DispatchQueue.global().async {
    box.value += 1
}

DispatchQueue.global().async {
    box.value += 1
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ir0nstone.gitbook.io/notes/binexp/memory-safety/swift.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
