Swift 扩展

扩展可以做以下功能

  • 添加计算属性
  • 添加类方法和实例方法
  • 添加新的初始化函数
  • 添加下标方法
  • 添加和使用嵌套类型
  • 遵守新的协议

添加计算属性

import UIKit

// 添加计算属性
extension Double {
    var km: Double {
        return self * 1_000.0
    }
    
    var m: Double {
        return self
    }
    
    var cm: Double {
        return self / 100.0
    }
    
    var mm: Double {
        return self / 1_000.0
    }
    
    var ft: Double {
        return self / 3.28084
    }
}

let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")

let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")


let aMarathon = 42.km + 195.m
print("A marathon is \(aMarathon) meters long")

console log 如下


添加计算属性.png

注意:扩展能添加计算属性,但是不能添加存储属性,也不能对现有属性添加属性值观察
添加新的初始化方法

// 添加新的初始化方法
struct Size {
    var width = 0.0, height = 0.0
}

struct Point {
    var x = 0.0, y = 0.0
}

struct Rect {
    var origin = Point()
    var size = Size()
}

let defaultRect = Rect()
let memberwiseRect = Rect(origin: Point(x: 2.0, y: 2.0), size: Size(width: 5.0, height: 5.0))

print("defaultRect origin is (\(defaultRect.origin.x), \(defaultRect.origin.y)) and size is (\(defaultRect.size.width),\(defaultRect.size.height))")
print("memberwiseRect origin is (\(memberwiseRect.origin.x), \(memberwiseRect.origin.y)) and size is (\(memberwiseRect.size.width),\(memberwiseRect.size.height))")

extension Rect {
    init(center: Point, size: Size) {
        let originX = center.x - size.width / 2.0
        let originY = center.y - size.height / 2.0
        
        self.init(origin: Point(x: originX, y: originY), size: size)
        
    }
}

let centerRect = Rect(center: Point(x: 4.0, y: 4.0), size: Size(width: 3.0, height: 3.0))

print("centerRect origin is (\(centerRect.origin.x), \(centerRect.origin.y)) and size is (\(centerRect.size.width),\(centerRect.size.height))")

console log 如下


添加新的初始化方法.png

添加实例方法

// 添加实例方法
extension Int {
    func repetitions(task: () -> Void){
        for _ in 0..

console log 如下


添加实例方法.png

可变实例方法

// 可变实例方法
extension Int {
    mutating func square() {
        self = self * self
    }
}

var someInt = 3
print("someInt is \(someInt)")
someInt.square()
print("someInt now is \(someInt)")

console log 如下


可变实例方法.png

添加下标

// 添加下标
extension Int {
    subscript(digitIndex: Int) -> Int {
        var decimalBase = 1
        for _ in 0..

console log 如下


Swift 扩展_第1张图片
添加下标.png

添加和使用嵌套类型

// 添加和使用嵌套类型
extension Int {
    enum Kind {
        case negative, zero, positive
    }
    
    var kind: Kind {
        switch self {
        case 0:
            return .zero
        case let x where x > 0:
            return .positive
        default:
            return .negative
        }
    }
}

func printIntegerKinds(numbers: [Int]){
    for number in numbers {
        switch number.kind {
        case .negative:
            print("-", terminator:"")
        case .zero:
            print("0", terminator:"")
        case .positive:
            print("+", terminator:"")
        }
    }
    print("")
}

printIntegerKinds([1992, 0, -8, 520, -5])

console log 如下


添加和使用嵌套类型.png

你可能感兴趣的:(Swift 扩展)