<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTUnwrap(_:_:file:line:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "s:6XCTest9XCTUnwrap__4file4linexxSgyKXK_SSyXKs12StaticStringVSutKlF"
  },
  "title" : "XCTUnwrap(_:_:file:line:)"
}
-->

# XCTUnwrap(_:_:file:line:)

Asserts that an expression is not `nil,` and returns the unwrapped value.

```
func XCTUnwrap<T>(_ expression: @autoclosure () throws -> T?, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) throws -> T
```

## Parameters

`expression`

An expression of type `T?`. The expression’s type determines the type of the return value.

`message`

An optional description of a failure.

`file`

The file where the failure occurs. The default is the filename of the test case where you call this function.

`line`

The line number where the failure occurs. The default is the line number where you call this function.

## Return Value

The result of evaluating and unwrapping the `expression`, which is of type `T`. `XCTUnwrap()` only returns a value if `expression` is not `nil`.

## Discussion

This function generates a failure when `expression` is `nil`. Otherwise, it returns the unwrapped value of `expression` for subsequent use in the test.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)