---
title: "withExtendedLifetime(_:_:)"
framework: swift
role: symbol
role_heading: Function
path: "swift/withextendedlifetime(_:_:)-4mmpv"
---

# withExtendedLifetime(_:_:)

Evaluates a closure while ensuring that the given instance is not destroyed before the closure returns.

## Declaration

```swift
func withExtendedLifetime<T, E, Result>(_ x: borrowing T, _ body: () throws(E) -> Result) throws(E) -> Result where E : Error, T : ~Copyable, T : ~Escapable, Result : ~Copyable
```

## Parameters

- `x`: An instance to preserve until the execution of body is completed.
- `body`: A closure to execute that depends on the lifetime of x being extended. If body has a return value, that value is also used as the return value for the withExtendedLifetime(_:_:) method.

## Return Value

Return Value The return value, if any, of the body closure parameter.

## See Also

### Reference Counting

- [Unmanaged](swift/unmanaged.md)
- [withExtendedLifetime(_:_:)](swift/withextendedlifetime(_:_:)-59dz3.md)
- [extendLifetime(_:)](swift/extendlifetime(_:).md)
