<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Optional/~=(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Operator",
  "symbol" : {
    "kind" : "Operator",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SqsRi_zRi0_zrlE2teoiySbs26_OptionalNilComparisonTypeV_xSgtFZ"
  },
  "title" : "~=(_:_:)"
}
-->

# ~=(_:_:)

Returns a Boolean value indicating whether an argument matches `nil`.

```
static func ~= (lhs: _OptionalNilComparisonType, rhs: borrowing Wrapped?) -> Bool
```

## Parameters

`lhs`

A `nil` literal.

`rhs`

A value to match against `nil`.

## Discussion

You can use the pattern-matching operator (`~=`) to test whether an
optional instance is `nil` even when the wrapped value’s type does not
conform to the `Equatable` protocol. The pattern-matching operator is used
internally in `case` statements for pattern matching.

The following example declares the `stream` variable as an optional
instance of a hypothetical `DataStream` type, and then uses a `switch`
statement to determine whether the stream is `nil` or has a configured
value. When evaluating the `nil` case of the `switch` statement, this
operator is called behind the scenes.

```
var stream: DataStream? = nil
switch stream {
case nil:
    print("No data stream is configured.")
case let x?:
    print("The data stream has \(x.availableBytes) bytes available.")
}
// Prints "No data stream is configured."
```

> Note: To test whether an instance is `nil` in an `if` statement, use the
> equal-to operator (`==`) instead of the pattern-matching operator. The
> pattern-matching operator is primarily intended to enable `case`
> statement pattern matching.

---

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)