Skip to content

Instantly share code, notes, and snippets.

@loganmoseley
Created January 26, 2018 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganmoseley/203bd176db2198d38688ed00b7985c16 to your computer and use it in GitHub Desktop.
Save loganmoseley/203bd176db2198d38688ed00b7985c16 to your computer and use it in GitHub Desktop.
Using `switch` to find a substring.
import Cocoa
struct Contains : ExpressibleByStringLiteral {
let s: String
static func ~= (l: Contains, r: Contains) -> Bool {
return r.s.contains(l.s)
}
init(_ c: String) {
s = c
}
init(stringLiteral value: String) {
s = value
}
}
extension String {
var contains: Contains {
return Contains(self)
}
}
var str = "Hello, playground"
switch str.contains {
case "str": print(0)
case "Hello": print(1)
case "play": print(2)
default: print(3)
}
@loganmoseley
Copy link
Author

Prints 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment