Published: 25 Jan 2019 › Updated: 25 Jan 2019![[Grammer] 25 - Operator Methods](/nhc-explorer-cover.webp)
![[Grammer] 25 - Operator Methods](/nhc-explorer-cover.webp)
[Grammer] 25 - Operator Methods
- Operator Methods
static func operator(parameters) -> ReturnType {
Code
}
struct Position {
var x : Double
var y : Double
}
extension Position {
static func ==(left: Position, right: Positon) -> Bool {
return left.x == right.x && left.y == right.y
}
static func !=(left: Position, right: Position) -> Bool {
return !(left == right)
}
}
let a = Position(x: 1, y: 2)
let b = Position(x: 3, y: 4)
a == b // false
a != b // true
extension Position {
static prefix func -(pos: Position) -> Position {
return Position(x: -pos.x, y: -pos.u)
}
static postfix func ++(pos: inout Position) -> Position {
let ret = pos
pos.x += 1
pos.y += 1
return ret
}
}
var a = Position(x: 1, y: 2)
var result = -a
dump(result)
result = a++
dump(result)
dumpt(a)Leave [Grammer] 25 - Operator Methods to:
Read more #swift4 posts
Best Posts From gunw
We have not curated any of gunw's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From gunw
- [Grammer] 26 - Custom Operators
- [Grammer] 25 - Operator Methods
- [Grammer] 23 - Bitwise Operators, 24 - Identity Operator
- [Grammer]21 - Ternary Coditional Operator, 22 - Range Operators
- [Grammer] 19 - Short-circuit Evaluation, Side Effect, 20 - Assignment Operators
- [Grammer] 16 - Overflow Operators, 17 - Comparison Operators, 18 - Logical Operators
- [Grammer] 13 - Data Types with Memory, 14 - Operator Basics, 15 - Arithmetic Operators
- [Grammer] 11 - Type Inference, Type Safety, 12 - Type Conversion, Type Aliases
- [Grammer] 09 - Boolean, 10 - Strings and Characters
- [Grammer] 07 - Naming Convention, 08 - Numbers