Skip to content

Instantly share code, notes, and snippets.

@albtsantos
Created August 3, 2014 02:43
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 albtsantos/2d486714ec14bf1109b9 to your computer and use it in GitHub Desktop.
Save albtsantos/2d486714ec14bf1109b9 to your computer and use it in GitHub Desktop.
Minus infix for Swift strings
@infix func - (left: String, right: String) -> String {
var finalString = left
if left.hasSuffix(right) {
// the workaround to the compiler error is to cast the String to NSString before the substringToIndex method
var index = countElements(left) - countElements(right)
finalString = (left as NSString).substringToIndex(index)
}
return finalString
}
"Apple Computers" - " Computers"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment