I recently downloaded the Advanced NSOperations sample app from Apple and found this code...
// Operators to use in the switch statement.
pr
It is an operator used for pattern matching in a case
statement.
You can take a look here to know how you can use and leverage it providing your own implementation:
Here is a simple example of defining a custom one and using it:
struct Person {
let name : String
}
// Function that should return true if value matches against pattern
func ~=(pattern: String, value: Person) -> Bool {
return value.name == pattern
}
let p = Person(name: "Alessandro")
switch p {
// This will call our custom ~= implementation, all done through type inference
case "Alessandro":
print("Hey it's me!")
default:
print("Not me")
}
// Output: "Hey it's me!"
if case "Alessandro" = p {
print("It's still me!")
}
// Output: "It's still me!"