Created
August 3, 2014 02:43
-
-
Save albtsantos/2d486714ec14bf1109b9 to your computer and use it in GitHub Desktop.
Minus infix for Swift strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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