Swift笔记 - 11.溢出运算符、Equatable、Comparable、自定义运算符、Cus
Swift笔记 - 11.溢出运算符、Equatable、Comparable、自定义运算符、CustomStringConvertible 与 CustomDebugStringConvertible、大写Self、assert、fatalError
Swift中的溢出运算符
- Swift的算数运算符出现溢出时会抛出运行时错误
- Swift有溢出运算符(
&+
、&-
、&*
),用来支持溢出运算
var min = UInt8.min
print(min &- 1) // 255, Int8.max
var max = UInt8.max
print(max &+ 1) // 0, Int8.min
print(max &* 2) //254,等价于 max &+ max
运算符重载
- 类、结构体、枚举可以为现有的运算符提供自定义的实现,这个操作叫做:运算符重载
struct Point {
var x: Int, y: Int
}
func + (p1: Point, p2: Point) -> Point {
Point(x: p1.x + p2.x, y: p1.y + p2.y)
}
let p = Point(x: 10, y: 20) + Point(x: 11, y: 22)
print(p) // Point (x: 21, y: 42)
struct Point {
var x: Int, y: Int
static func + (p1: Point, p2: Point) -> Point {
Point (x: p1.x + p2.x, y: p1.y + p2.y)
}
}
static func + (p1: Point, p2: Point) -> Point {
Point (x: p1.x + p2.x, y: p1.y + p2.y)
}
static func - (p1: Point, p2: Point) -> Point {
Point(x: p1.x - p2.x, y: p1.y - p2.y)
}
static prefix func - (p: Point) -> Point {
Point (x: -p.x, y: -p.y)
}
static func += (p1: inout Point, p2: Point) {
p1 = p1 + p2
}
static prefix func ++ (p: inout Point) -> Point {
p += Point(x: 1, y: 1)
return p
}
static postfix func ++ (p: inout Point) -> Point {
let tmp = p
p += Point(x: 1, y: 1)
return tmp
}
static func == (p1: Point, p2: Point) -> Bool {
(p1.x == p2.x) && (p1.y == p2.y)
}
Equatable
- 要想得知2个实例是否等价,一般做法是遵守Equatable协议,重载二=运算符
- 与此同时,等价于重载了!=运算符
struct Point: Equatable {
var x: Int, y: Int
}
var p1 = Point(x: 10, y: 20)
var p2 = Point(x: 11, y: 22)
print (p1 == p2) // false
print (p1 != p2) // true
- Swift为以下类型提供默认的
Equatable
实现- 没有关联类型的枚举
- 只拥有遵守
Equatable
协议关联类型的枚举 - 只拥有遵守
Equatable
协议存储厲性的结构体
- 引用类型比较存储的地址值是否相等(是否引用着同一个对象),使用恒等运算符
===
、!==
Comparable可比较
// score大的比较大,若score相等,age小的比较大
struct Student: Comparable {
var age: Int
var score: Int
init(score: Int, age: Int) {
self.score = score
self.age = age
}
static func < (lhs: Student, rhs: Student) -> Bool {
(lhs < rhs.score) || (lhs == rhs.score && lhs.age > rhs.age)
}
static func > (lhs: Student, rhs: Student) -> Bool {
(lhs > rhs.score) || (lhs == rhs.score && lhs.age < rhs.age)
}
static func <= (lhs: Student, rhs: Student) -> Bool {
!(lhs > rhs)
}
static func >= (lhs: Student, rhs: Student) -> Bool {
! (lhs < rhs)
}
}
- 要想比较2个实例的大小,一般做法是:
- 遵守
Comparable
协议 - 重载相应的运算符
- 遵守
var stu1 = Student(score: 100, age: 20)
var stu2 = Student(score: 98, age: 18)
var stu3 = Student(score: 100, age: 20)
print(stul > stu2) // true
print(stul >= stu2) // true
print(stul >= stu3) // true
print(stul <= stu3) // true
print(stuz < stu1) // true
print(stu2 <= stul) // true
自定义运算符
- 可以自定义新的运算符:在全局作用域使用
operator
进行声明
prefix operator 前缀运算符
postfix operator 后缀运算符
infix operator 中级运算符:优先级组
precedencegroup 优先级组 {
associativity:结合性(left\right\none)
higherThan:比谁的优先级高
lowerThan:比谁的优先级低
assignment:true 代表在可选链操作中拥有跟赋值运算符一样的优先级
}
prefix operator +++
infix operator +- : PlusMinusPrecedence
precedencegroup PlusMinusPrecedence {
associativity: none
higherThan: AdditionPrecedence
lowerThan: MultiplicationPrecedence
assignment: true
}
- Apple文档参考:
struct Point {
var x: Int, y: Int
static prefix func +++ (point: inout Point) -> Point {
point = Point(x: point.x + point.x, y: point.y + point.y)
return point
}
static func +- (left: Point, right: Point) -> Point {
return Point(x: left.x + right.x, y: left.y - right.y)
}
static func +- (left: Point?, right: Point) -> Point {
print ("+-")
return Point(x: left?.x ?? 0 + right.x, y: left?.y ?? 0 - right.y)
}
}
struct Person {
var point: Point
}
var person: Person? = nil
person?.point +- Point (x: 10, y: 20)
CustomStringConvrtible 与 CustomDebugStringConvertible
- 遵守
CustomStringConvertible
、CustomDebugStringConvertible
协议,都可以自定义实例的打印字符串
class Person : CustomStringConvertible, CustomDebugStringConvertible {
var age = 0
var description: String { "person \(age)" }
var debugDescription: String { "debug person \(age)" }
}
var person = Person()
print(person) // person_0
debugPrint(person) // debug person_0
print
调用的是CustomStringConvertible
协议的description
debugPrint
,po
调用的是CustomDebugStringConvertible
协议的debugDescription
大写Self
Self
代表当前类型
class Person {
var age = 1
static var count = 2
func run() {
print(self.age) // 1
print(Self.count) // 2
}
}
Self
一般用作返回值类型,限定返回值跟方法调用者必领是同一类型(也可以作为参数类型)
protocol Runnable {
func test() -> Self
}
class Person : Runnable {
required init() {}
func test() -> Self { type(of: self).init() }
}
class Student : Person()
var p = Person()
// Person
print(p.test())
var stu = Student()
// Student
print(stu.test())
assert(断言)
- 很多编程语言都有断言机制:不符合指定条件就抛出运行时错误,常用于调试(
Debug
)阶段的条件判断 - 默认情況下,
Swift
的断言只会在Debug模式下生效,Release
模式下会忽路
func divide(_ v1: Int, _ v2: Int) -> Int {
assert(v2 != 0,"除数不能为0")
return v1 / v2
}
print(divide(20, 0))
- 增加
Swift Flags
修改断言的默认行为-assert-config Release
:强制关闭断言-assert-config Debug
:强制开启断言
fatalError
- 如果遇到严重问题,希望结束程序运行时,可以直接使用
fatalError
函数地出错误(这是无法通过do-catch
捕捉的错误) - 使用了
fatalError
函数,就不需要再写return
func test(_ num: Int) -> Int {
if num >= 0 {
return 1
}
fatalError("numfaF0")
}
- 在某些不得不实现、但不希望别人调用的方法,可以考虑内部使用
fatalError
函数
class Person { required init() {} }
class Student: Person {
required init() { fatalError("don't call Student.init") }
init(score: Int) {}
}
var stul = Student(score: 98)
var stu2 = Student()