“As” type-casting pattern

The as operator combines the is type casting pattern with the value-binding pattern. Extending the example above, you could write a case like this:

for element in array { switch element {

case let text as String:

print("Found a string: (text)") // 1 time default:

print("Found something else") // 2 times

}

}

So when the compiler finds an object that it can cast to a String, the compiler will bind the value to the text constant.

results matching ""

    No results matching ""