Swift之方法

本文首发于我的博客html

前言

方法,也就是函数。同其余语言同样,在Swift中,也是分为实例方法和类型方法git

枚举、结构体、类均可以定义实例方法、类型方法

实例方法(Instance Method):经过实例对象调用

类型方法(Type Method):经过类型调用,用static或者class关键字定义,相似OC中的类方法

例若有个类Car,有实例方法getnNum和类型方法getCountgithub

class Car {
    static var cout = 0
    var num = 0
    init() {
        Car.cout += 1
    }
    // 类型方法
    static func getCount() -> Int { cout }
    // 实例方法
    func getnNum() -> Int {
        num
    }
}
let c0 = Car()
let c1 = Car()
let c2 = Car()
print(Car.getCount()) // 3

c0.num = 10
c1.num = 11

print(c1.num) //11
print(c2.num) //0
复制代码

self

无论是实例方法,仍是类型方法,里面均可以调用 self编程

  • 在实例方法中表明实例对象
  • 在类型方法中表明类型
    • 在类型方法static func getCount中
    • cout等价于self.cout、Car.self.cout、Car.cout

例如上面的代码能够写成bash

class Car {
    static var cout = 0
    var num = 0
    init() {
        Car.cout += 1
    }
    // 类型方法
    static func getCount() -> Int {
        self.cout //self表明类型
    }
    
    func getnNum() -> Int {
        self.num //self表明实例
    }
}
复制代码

关键字mutating

  • 结构体和枚举是值类型,默认状况下,值类型的属性不能被自身的实例方法修改
  • 在func关键字前加mutating能够容许这种修改行为

eg:app

struct Point {
    var x = 0.0, y = 0.0
     func moveBy(deltaX: Double, deltaY: Double) {
        x += deltaX //编译报错 Left side of mutating operator isn't mutable: 'self' is immutable y += deltaY //编译报错 Left side of mutating operator isn't mutable: 'self' is immutable
         self = Point(x: x + deltaX, y: y + deltaY) //编译报错 Cannot assign to value: 'self' is immutable
    }
}



enum StateSwitch {
    case low, middle, high
     func next() {
        switch self {
        case .low:
            self = .middle//编译报错 Cannot assign to value: 'self' is immutable
        case .middle:
            self = .high//编译报错 Cannot assign to value: 'self' is immutable
        case .high:
            self = .low//编译报错 Cannot assign to value: 'self' is immutable
        }
    }
}
复制代码
  • 加上关键字mutating以后就能够了
struct Point {
    var x = 0.0, y = 0.0
    mutating func moveBy(deltaX: Double, deltaY: Double) {
        x += deltaX 
        y += deltaY 
        self = Point(x: x + deltaX, y: y + deltaY) 
      }
}



enum StateSwitch {
    case low, middle, high
   mutating  func next() {
        switch self {
        case .low:
            self = .middle
        case .middle:
            self = .high
        case .high:
            self = .low        }
    }
}
复制代码

关键字@discardableResult

  • 在func前面加个@discardableResult,能够消除:函数调用后返回值未被使用的警告⚠

eg:ide

struct Point {
    var x = 0.0, y = 0.0
     mutating func moveX(deltaX: Double) -> Double {
        x += deltaX
        return x
    }
}
var p = Point()
p.moveX(deltaX: 10)
复制代码

由于方法moveX的返回值没有使用,编译器会报警告函数

  • 若是加了关键字@discardableResult就不会警告了
struct Point {
    var x = 0.0, y = 0.0
    @discardableResult mutating 
    func moveX(deltaX: Double) -> Double {
        x += deltaX
        return x
    }
}
var p = Point()
p.moveX(deltaX: 10)
复制代码

参考资料:ui

Swift官方源码spa

从入门到精通Swift编程

相关文章
相关标签/搜索