Operator 演算子

~=(_:_:)

Returns a Boolean value indicating whether a value is included in a range. 値が範囲に含まれるかどうかを指し示すブール値を返します。

Declaration 宣言

static func ~= (pattern: PartialRangeThrough<Bound>, value: Bound) -> Bool

Parameters パラメータ

pattern

A range. 範囲。

bound

A value to match against pattern. patternと照合することになるある値。

Discussion 解説

You can use the pattern-matching operator (~=) to test whether a value is included in a range. The pattern-matching operator is used internally in case statements for pattern matching. The following example uses the ~= operator to test whether an integer is included in a range of single-digit numbers: あなたは、このパターンマッチング演算子(~=)を使うことで、ある値がある範囲に含まれるかどうか検査できます。パターンマッチング演算子は、内部的にcase文の中でパターンマッチングのために使われます。以下の例は、~=演算子を使って、ある整数が一桁の数いくつかからなるある範囲に含まれるかどうか調べます:


let chosenNumber = 3
if 0..<10 ~= chosenNumber {
    print("\(chosenNumber) is a single digit.")
}
// Prints "3 is a single digit."