# \#defer-statement

**URL:** https://forums.swift.org/tag/defer-statement/421.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [Can I choose what block a \`defer\` statements belongs to?](https://forums.swift.org/t/can-i-choose-what-block-a-defer-statements-belongs-to/72586)

<div class="topic-metadata">

**Author:** [@anselmschueler](https://forums.swift.org/u/anselmschueler)\
**Replies:** 10\
**Last updated:** [June 22, 2024, 9:54am UTC](https://forums.swift.org/t/can-i-choose-what-block-a-defer-statements-belongs-to/72586 "2024-06-22T09:54:46Z")

</div>

Suppose I have a function like this: func disposeController(\_ controller: Controller) { let requiresDisconnection; if controller.isConnected { requiresDisconnection = true gameManager.prepareHand…

---

## [A combination of defer-statement and closure may cause crash](https://forums.swift.org/t/a-combination-of-defer-statement-and-closure-may-cause-crash/71692)

<div class="topic-metadata">

**Author:** [@xrloong](https://forums.swift.org/u/xrloong)\
**Replies:** 0\
**Last updated:** [May 8, 2024, 10:21am UTC](https://forums.swift.org/t/a-combination-of-defer-statement-and-closure-may-cause-crash/71692 "2024-05-08T10:21:25Z")

</div>

Here is the code causing crash: import Foundation let KEY = "key" func doHeavyWork() { print("Enter doHeavyWork") defer { print("Leave doHeavyWork") } defer { let callback: () -\> () = { …

---

## [Targeting a specific scope with defer](https://forums.swift.org/t/targeting-a-specific-scope-with-defer/69942)

<div class="topic-metadata">

**Author:** [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Replies:** 2\
**Last updated:** [April 18, 2024, 2:20am UTC](https://forums.swift.org/t/targeting-a-specific-scope-with-defer/69942 "2024-04-18T02:20:05Z")

</div>

Is there a way to specify which scope a deferred block should run at the end of, rather than just the current scope? e.g. I might want to write: var data: UnsafeMutablePointer\<UInt8\> if someCondition { data = some…

---

## [Defer and "anonymous closure argument not contained in a closure" error](https://forums.swift.org/t/defer-and-anonymous-closure-argument-not-contained-in-a-closure-error/64122)

<div class="topic-metadata">

**Author:** [@Loooop](https://forums.swift.org/u/Loooop)\
**Replies:** 0\
**Last updated:** [April 1, 2023, 8:26am UTC](https://forums.swift.org/t/defer-and-anonymous-closure-argument-not-contained-in-a-closure-error/64122 "2023-04-01T08:26:44Z")

</div>

Sometimes I get really baffled by Swift's compiler. Why doesn't B compile? Why can't I put an anonymous closure argument inside a defer block? struct Example { var value = 0 mutating func doSomething( \_: (inout Se…

---

## [How to (kind of) emulate RAII using \`defer\`](https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719)

<div class="topic-metadata">

**Author:** [@vitamin](https://forums.swift.org/u/vitamin)\
**Replies:** 6\
**Last updated:** [March 15, 2023, 4:35pm UTC](https://forums.swift.org/t/how-to-kind-of-emulate-raii-using-defer/63719 "2023-03-15T16:35:40Z")

</div>

Swift's defer statement has a nice capability to kind of emulate C++'s Resource Acquisition Is Initialization behavior, which we don't get due to ARC. This could be handy if you're doing something with UnsafeMutablePoint…

---

## [A bug? Can't defer actor-isolated variable access](https://forums.swift.org/t/a-bug-cant-defer-actor-isolated-variable-access/50796)

<div class="topic-metadata">

**Author:** [@Agent](https://forums.swift.org/u/Agent)\
**Replies:** 21\
**Last updated:** [August 21, 2022, 11:43pm UTC](https://forums.swift.org/t/a-bug-cant-defer-actor-isolated-variable-access/50796 "2022-08-21T23:43:46Z")

</div>

Hey, I'm seeing this issue using the compiler in Xcode Version 13.0 beta 3 (13A5192j) class ViewController: UIViewController { var value : String? = "Hello" @IBAction func showAlert(\_ sender: Any) { Task…

---

## [Adding sugar for precondition defer](https://forums.swift.org/t/adding-sugar-for-precondition-defer/52726)

<div class="topic-metadata">

**Author:** [@bnyu](https://forums.swift.org/u/bnyu)\
**Replies:** 0\
**Last updated:** [October 11, 2021, 6:28am UTC](https://forums.swift.org/t/adding-sugar-for-precondition-defer/52726 "2021-10-11T06:28:48Z")

</div>

We can trigger defer until after some preconditions like: guard something allocate memory; defer {release memory} What if we want whether to trigger deffer according to the condition and continue execute something, c…

---

## [$functionReturnValue](https://forums.swift.org/t/functionreturnvalue/39888)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 24\
**Last updated:** [September 5, 2020, 10:27pm UTC](https://forums.swift.org/t/functionreturnvalue/39888 "2020-09-05T22:27:33Z")

</div>

hello, when using defer statement it is very tempting to have an ability to grab the function return value without introducing a special variable and tracking it manually. consider this example: func foo1(param1: Int, …
