Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

~= operator in Swift

I recently downloaded the Advanced NSOperations sample app from Apple and found this code...

// Operators to use in the switch statement. private func ~=(lhs: (String, Int, String?), rhs: (String, Int, String?)) -> Bool {     return lhs.0 ~= rhs.0 && lhs.1 ~= rhs.1 && lhs.2 == rhs.2 }  private func ~=(lhs: (String, OperationErrorCode, String), rhs: (String, Int, String?)) -> Bool {     return lhs.0 ~= rhs.0 && lhs.1.rawValue ~= rhs.1 && lhs.2 == rhs.2 } 

It seems to use the ~= operator against Strings and Ints but I've never seen it before.

What is it?

like image 789
Fogmeister Avatar asked Jul 14 '16 10:07

Fogmeister


People also ask

What symbol means in Swift?

Simply use a shortcut to "range": you can construct a range and "~=" means "contains". ( other can add more theoretical details, but the sense is this). Read it as "contains" let n: Int = 100 // verify if n is in a range, say: 10 to 100 (included) if n>=10 && n<=100 { print("inside!") } // using "patterns" if 10...

What is nil coalescing operator in Swift?

Swift's nil coalescing operator helps you solve this problem by either unwrapping an optional if it has a value, or providing a default if the optional is empty. Because name is an optional string, we need to unwrap it safely to ensure it has a meaningful value.

What is infix operator in Swift?

Types of Operators Operators in swift fall into the following types: Infix — Used between two values (ex: <value>+<value>) Prefix — Used before a value (ex: ! <value>) Postfix — Used after a value (ex: <value>!)

What is operator overloading in Swift?

Swift supports operator overloading, which is a fancy way of saying that what an operator does depends on the values you use it with. For example, + sums integers like this: let meaningOfLife = 42 let doubleMeaning = 42 + 42. But + also joins strings, like this: let fakers = "Fakers gonna " let action = fakers + "fake"


2 Answers

Simply use a shortcut to "range": you can construct a range and "~=" means "contains". (other can add more theoretical details, but the sense is this). Read it as "contains"

let n: Int = 100  // verify if n is in a range, say: 10 to 100 (included)  if n>=10 && n<=100 {     print("inside!") }  // using "patterns" if 10...100 ~= n {     print("inside! (using patterns)")  } 

try with some values of n.

Is used widely for example in HTTP response:

if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {                 let contentLength : Int64 = response.expectedContentLength                 completionHandler(contentLength)             } else {                 completionHandler(nil) 
like image 64
ingconti Avatar answered Sep 24 '22 10:09

ingconti


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:

  • http://oleb.net/blog/2015/09/swift-pattern-matching/
  • http://austinzheng.com/2014/12/17/custom-pattern-matching/

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!" 
like image 39
Alessandro Orrù Avatar answered Sep 26 '22 10:09

Alessandro Orrù