Swift自定义操作符
在Swift
中,自定义操作符就是简单的二步:首先在全局使用operator
关键字来声明操作符,同时用prefix
、infix
或postfix
来声明操作符的位置;然后在所需要的类/结构体中实现操作符。如下代码所示:
postfix operator >?
postfix operator >!
extension MIType {
public static postfix func >?(type: MIType) -> MIType {
return MIType("Optional<\(type.name)>")
}
public static postfix func >!(type: MIType) -> MIType {
return MIType("ImplicitlyUnwrappedOptional<\(type.name)>")
}
}
自定义操作符需要以两类字符开头:
ASCII
字符中的/, =, -, +, !, *, %, <, >, &, |, ^, ?, ~
Unicode
中的Mathematical Operators
,Miscellaneous Symbols
和Dingbats Unicode blocks
这些字符中的字符
然后后面允许使用组合的Unicode
字符。如下代码是以一个Miscellaneous Symbols
开头的实现向量加法的操作符。
infix operator ★+
struct Vector2D {
var x: CGFloat
var y: CGFloat
}
extension Vector2D {
static func ★+ (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y + right.y)
}
}
let vector1 = Vector2D(x: 10, y: 20)
let vector2 = Vector2D(x: 30, y: 10)
let vector = vector1 ★+ vector2
vector.x // 40.0
vector.y // 30.0
参考
- The Swift Programming Language (Swift 4) – Advanced Operators
- [The Swift Programming Language (Swift 4) – Lexical Structure](