Last active
November 11, 2018 09:49
-
-
Save hamada147/d0db6d1df99c3b664d752c584670fb5a to your computer and use it in GitHub Desktop.
Add ++ & -- operators to Int in Swift 4
This file contains hidden or 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 operator ++ | |
infix operator -- | |
extension Int { | |
/// decress the value by one | |
/// | |
/// - Parameter num: value to decress | |
static prefix func --(_ num: inout Int) -> Int { | |
num -= 1 | |
return num | |
} | |
/// decress the value by one | |
/// | |
/// - Parameter num: value to decress | |
static postfix func --(_ num: inout Int) -> Int { | |
let oldValue = num | |
num -= 1 | |
return oldValue | |
} | |
/// increase the value by one | |
/// | |
/// - Parameter num: value to increase | |
static prefix func ++(_ num: inout Int) -> Int { | |
num += 1 | |
return num | |
} | |
/// increase the value by one | |
/// | |
/// - Parameter num: value to increase | |
static postfix func ++(_ num: inout Int) -> Int { | |
let oldValue = num | |
num += 1 | |
return oldValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment