> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/appendix/testing-system/xctest/xctunwrap.md).

# XCTUnwrap

{% tabs %}
{% tab title="💈範例" %}
{% hint style="info" %} <mark style="color:red;">**Unwrap**</mark> the value, or <mark style="color:red;">**throw**</mark> an error.
{% endhint %}

```swift
//                  ╭──⭐️──╮
func testUserToken() throws {
    let user = User()
    // if succeeds: get the value, if fails: throw an exception.
    let token = try XCTUnwrap(user.getToken())    // ⭐️ can throw
    XCTAssertEqual(token.count, 40)
}
```

{% endtab %}

{% tab title="🔸 定義" %}

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

{% endtab %}

{% tab title="📗 參考" %}

* [x] HwS ⟩ [How to check and unwrap optionals in tests using XCTUnwrap()](https://www.hackingwithswift.com/example-code/testing/how-to-check-and-unwrap-optionals-in-tests-using-xctunwrap)
  {% endtab %}

{% tab title="📘 手冊" %}

* [XCTest](https://developer.apple.com/documentation/xctest) ⟩ [Nil and Non-Nil Assertions](https://developer.apple.com/documentation/xctest/nil_and_non-nil_assertions) ⟩
  * [XCTUnwrap(\_:\_:file:line:)](https://developer.apple.com/documentation/xctest/3380195-xctunwrap)
  * [XCTAssertNotNil(*:*:file:line:)](https://developer.apple.com/documentation/xctest/1500453-xctassertnotnil)
    {% endtab %}
    {% endtabs %}
