Swift之運(yùn)算符重載示例詳解
引言
在C++中, 運(yùn)算符是可以重載的, Swift也是支持的
注:=和三目運(yùn)算符a ? b : c不可重載
重載雙目運(yùn)算符
class Vector {
var x: Double = 0.0
var y: Double = 0.0
var z: Double = 0.0
init(x: Double, y: Double, z: Double) {
self.x = x
self.y = y
self.z = z
}
convenience init() {
self.init(x: 0, y: 0, z: 0)
}
/// Operator '+' declared in type 'Vector' must be 'static'
/// + 是雙目運(yùn)算符,兩個(gè)參數(shù)
static func +(v1: Vector, v2: Vector) -> Vector {
let vector: Vector = Vector()
vector.x = v1.x + v2.x
vector.y = v1.y + v2.y
vector.z = v1.z + v2.z
return vector
}
}單目運(yùn)算符
類和結(jié)構(gòu)體也可以實(shí)現(xiàn)單目運(yùn)算符,單目運(yùn)算符只運(yùn)算一個(gè)值。
運(yùn)算符出現(xiàn)在值之前為前綴運(yùn)算符(prefix, 如 -a),出現(xiàn)在值之后為后綴運(yùn)算符(postfix, 如a!)。
struct Coordinate {
var x: Double = 0.0
var y: Double = 0.0
static prefix func +(coordinate: Coordinate) -> Coordinate {
return Coordinate(x: +coordinate.x, y: +coordinate.y)
}
static prefix func -(coordinate: Coordinate) -> Coordinate {
return Coordinate(x: -coordinate.x, y: -coordinate.y)
}
}
func test() {
let p1 = Coordinate(x: 1.0, y: 1.0)
let p2 = Coordinate(x: 2.0, y: 2.0)
print(-p1) // Coordinate(x: -1.0, y: -1.0)
print(+p2) // Coordinate(x: 2.0, y: 2.0)
}復(fù)合賦值運(yùn)算符
復(fù)合賦值運(yùn)算符是賦值運(yùn)算符(=)和其他運(yùn)算符進(jìn)行結(jié)合, 如 +=、-=
struct Coordinate {
var x: Double = 0.0
var y: Double = 0.0
// 如果放在全局函數(shù)里, 不需要加static, 否則, static不可省略
static func +=(left: inout Coordinate, right: Coordinate) {
left = Coordinate(x: left.x + right.x, y: left.y + right.y)
}
static func -=(c1: inout Coordinate, c2: Coordinate) {
left = Coordinate(x: left.x - right.x , y: left.y - right.y)
}
}
func test() {
var p1 = Coordinate(x: 1.0, y: 1.0)
let p2 = Coordinate(x: 2.0, y: 2.0)
p1 += p2
print(p1) // Coordinate(x: 3.0, y: 3.0)
}等價(jià)運(yùn)算符 ==
自定義類和結(jié)構(gòu)體沒有對等價(jià)運(yùn)算符進(jìn)行默認(rèn)的實(shí)現(xiàn)。等價(jià)運(yùn)算符一般被稱為相等運(yùn)算符==和不等運(yùn)算符 !=
struct Coordinate {
var x: Double = 0.0
var y: Double = 0.0
static func ==(c1: Coordinate, c2: Coordinate) -> Bool {
return c1.x == c2.x && c1.y == c2.y
}
static func !=(left: Coordinate, right: Coordinate) -> Bool {
return left.x != right.x || left.y != right.y
}
}
func test() {
let p1 = Coordinate(x: 1.0, y: 1.0)
var p2 = Coordinate(x: 2.0, y: 2.0)
print(p1 == p2) // false
p2.x -= 1
p2.y -= 1
print(p1 == p2) // true
}自定義運(yùn)算符
可以自定義前置運(yùn)算符prefix、后置運(yùn)算符postfix、中置運(yùn)算符infix, 約束條件:
- 只能使用如下字符:
- + * / = % < > ! & | ^ . ~ - 只有中置運(yùn)算符可以繼承優(yōu)先級(jí)組 precdence(后面介紹)
- 自定義運(yùn)算符不可放在類中,必須放在文件作用域中
自定義后置運(yùn)算符
struct Coordinate {
var x: Double = 0.0
var y: Double = 0.0
}
// 文件作用域,即全局作用域,不可在class下
prefix operator +++
prefix func +++(coordinate: inout Coordinate) -> Coordinate {
coordinate.x = coordinate.x + coordinate.x
coordinate.y = coordinate.y + coordinate.y
return coordinate
}
func test() {
var p1 = Coordinate(x: 1.0, y: 1.0)
print(+++p1) // Coordinate(x: 2.0, y: 2.0)
}自定義前置運(yùn)算符和自定義后置運(yùn)算符類似
postfix operator +++
postfix func +++(coordinate: inout Coordinate) -> Coordinate {
coordinate.x = coordinate.x + coordinate.x
coordinate.y = coordinate.y + coordinate.y
return coordinate
}自定義中置運(yùn)算符
precedencegroup MyPrecedence {
// higherThan: AdditionPrecedence // 優(yōu)先級(jí), 比加法運(yùn)算高
lowerThan: AdditionPrecedence // 優(yōu)先級(jí), 比加法運(yùn)算低
associativity: none // 結(jié)合方向:left, right or none
assignment: false // true代表是賦值運(yùn)算符,false代表非賦值運(yùn)算符
}
infix operator +++ : MyPrecedence // 繼承 MyPrecedence 優(yōu)先級(jí)組(可選)
func +++(left: Int, right: Int) -> Int {
return (left + right) * 2
}
func test() {
print(3+++2)
}借助自定義運(yùn)算符,我們可以浪一浪,定義一個(gè)操作符,當(dāng)有值是使用此值,無值時(shí)使用想要的默認(rèn)值,如下所示:
postfix operator <<<
postfix func <<<(a: String?) -> String { return a ?? "" }
func test() {
print(getStr()<<<)
}
func getStr() -> String? {
return nil
}示例
postfix operator <<<
postfix func <<<(a: String?) -> String { return a ?? "" }
postfix func <<<(a: Int?) -> Int { return a ?? 0 }
postfix func <<<(a: Int8?) -> Int8 { return a ?? 0 }
postfix func <<<(a: Int16?) -> Int16 { return a ?? 0 }
postfix func <<<(a: Int32?) -> Int32 { return a ?? 0 }
postfix func <<<(a: Int64?) -> Int64 { return a ?? 0 }
postfix func <<<(a: UInt?) -> UInt { return a ?? 0 }
postfix func <<<(a: Double?) -> Double { return a ?? 0.0 }
postfix func <<<(a: Float?) -> Float { return a ?? 0.0 }
postfix func <<<(a: [AnyObject]?) -> [AnyObject] { return a ?? [] }
postfix func <<<(a: [String]?) -> [String] { return a ?? [] }
postfix func <<<(a: [Int]?) -> [Int] { return a ?? [] }
postfix func <<<(a: [String: AnyObject]?) -> [String: AnyObject] { return a ?? [:]}
postfix func <<<(a: [String: String]?) -> [String: String] { return a ?? [:] }以上就是Swift之運(yùn)算符重載示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Swift運(yùn)算符重載的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
快速排序算法在Swift編程中的幾種代碼實(shí)現(xiàn)示例
快速排序是一種不穩(wěn)定的排序,存在著優(yōu)化空間,這里我們來看快速排序算法在Swift編程中的幾種代碼實(shí)現(xiàn)示例:2016-07-07
Swift開發(fā)應(yīng)用中如何更方便地使用顏色詳解
這篇文章主要給大家介紹了關(guān)于Swift開發(fā)應(yīng)用中如何更方便地使用顏色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Swift中優(yōu)雅處理閉包導(dǎo)致的循環(huán)引用詳解
這篇文章主要給大家介紹了關(guān)于Swift中優(yōu)雅的處理閉包導(dǎo)致的循環(huán)引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
在一個(gè)項(xiàng)目中同時(shí)使用Swift和Objective-C代碼混合編程的方法
這篇文章主要介紹了在一個(gè)項(xiàng)目中同時(shí)使用Swift和Objective-C代碼的方法,在一個(gè)工程中同時(shí)使用Swift和Objective-C混合語言編程的方法,需要的朋友可以參考下2014-07-07
淺析Swift中struct與class的區(qū)別(匯編角度底層分析)
這篇文章主要介紹了Swift中struct與class的區(qū)別 ,本文從匯編角度分析struct與class的區(qū)別,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

