Skip to content

Instantly share code, notes, and snippets.

@hamada147
Last active November 11, 2018 09:49
Show Gist options
  • Save hamada147/d0db6d1df99c3b664d752c584670fb5a to your computer and use it in GitHub Desktop.
Save hamada147/d0db6d1df99c3b664d752c584670fb5a to your computer and use it in GitHub Desktop.
Add ++ & -- operators to Int in Swift 4
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