Swift extension

Extensions are the addition of new functionality to an existing class, structure, or enumeration type.

Extensions can add new functionality to a type, but they cannot override existing functionality.

Extensions in Swift can:

  • Add calculated properties and calculated static properties
  • Define instance methods and type methods
  • A new constructor is available
  • Define the underlying
  • Define and use new nested types
  • Make an existing type conform to an agreement

Grammar

Extended claims use the keyword extension:

extension SomeType {
    // 加到SomeType的新功能写到这里
}

An extension can extend an existing type to fit one or more protocols in the following syntax format:

extension SomeType: SomeProtocol, AnotherProctocol {
    // 协议实现写到这里
}

Calculated properties

Extensions can add calculated instance properties and calculated type properties to existing types.

Instance

The following example adds five computational instance properties to the Int type and extends its functionality:

extension Int {
   var add: Int {return self + 100 }
   var sub: Int { return self - 10 }
   var mul: Int { return self * 10 }
   var div: Int { return self / 5 }
}
    
let addition = 3.add
print("加法运算后的值:\(addition)")
    
let subtraction = 120.sub
print("减法运算后的值:\(subtraction)")
    
let multiplication = 39.mul
print("乘法运算后的值:\(multiplication)")
    
let division = 55.div
print("除法运算后的值: \(division)")

let mix = 30.add + 34.sub
print("混合运算结果:\(mix)")

The output of the above program execution is:

加法运算后的值:103
减法运算后的值:110
乘法运算后的值:390
除法运算后的值: 11
混合运算结果:154

Constructor

Extensions can add new constructors to existing types.

This allows you to extend other types, use your own custom type as a constructor parameter, or provide additional initialization options that were not included in the original implementation of that type.

Extensions can add a new convenience constructor init() to a class, but they cannot add a new specified constructor or destructor deinit() to the class.

struct sum {
    var num1 = 100, num2 = 200
}

struct diff {
    var no1 = 200, no2 = 100
}

struct mult {
    var a = sum()
    var b = diff()
}

let calc = mult()
print ("mult 模块内 \(calc.a.num1, calc.a.num2)")
print("mult 模块内 \(calc.b.no1, calc.b.no2)")

let memcalc = mult(a: sum(num1: 300, num2: 500),b: diff(no1: 300, no2: 100))

print("mult 模块内 \(memcalc.a.num1, memcalc.a.num2)")
print("mult 模块内 \(memcalc.b.no1, memcalc.b.no2)")

extension mult {
    init(x: sum, y: diff) {
        _ = x.num1 + x.num2
        _ = y.no1 + y.no2
    }
}


let a = sum(num1: 100, num2: 200)
print("Sum 模块内:\( a.num1, a.num2)")


let b = diff(no1: 200, no2: 100)
print("Diff 模块内: \(b.no1, b.no2)")

The output of the above program execution is:

mult 模块内 (100, 200)
mult 模块内 (200, 100)
mult 模块内 (300, 500)
mult 模块内 (300, 100)
Sum 模块内:(100, 200)
Diff 模块内: (200, 100)

Method

Extensions can add new instance and type methods to existing types.

Here's an example of adding a new instance method called topics to the Int type:

extension Int {
   func topics(summation: () -> ()) {
      for _ in 0..<self {
         summation() 
      }
   }
}  

4.topics({
   print("扩展模块内")       
})    
    
3.topics({
   print("内型转换模块内")       
})  

The output of the above program execution is:

扩展模块内
扩展模块内
扩展模块内
扩展模块内
内型转换模块内
内型转换模块内
内型转换模块内

This topics uses a single argument () -> () that the function has no arguments and no return value.

Once you have defined the extension, you can call the topics method on topics implementing the ability to perform a task multiple times:


Variable instance method

You can also modify the instance itself by extending the instance method you added.

Methods that modify self or its properties in structures and enumeration types must label the instance method as mutting, just as they did from the original implementation.

Instance

The following example adds a new modification method called square to Swift's Double type to implement a square calculation of the original value:

extension Double {
   mutating func square() {
      let pi = 3.1415
      self = pi * self * self
   }
}

var Trial1 = 3.3
Trial1.square()
print("圆的面积为: \(Trial1)")


var Trial2 = 5.8
Trial2.square()
print("圆的面积为: \(Trial2)")


var Trial3 = 120.3
Trial3.square()
print("圆的面积为: \(Trial3)")

The output of the above program execution is:

圆的面积为: 34.210935
圆的面积为: 105.68006
圆的面积为: 45464.070735

The underse-label

Extensions can add a new underrug to an existing type.

Instance

The following example adds an integer to Swift's built-in type Int. The underse cursor, n, returns a hedding number

extension Int {
   subscript(var multtable: Int) -> Int {
      var no1 = 1
      while multtable > 0 {
         no1 *= 10
         --multtable
      }
      return (self / no1) % 10
   }
}
    
print(12[0])
print(7869[1])
print(786543[2])

The output of the above program execution is:

2
6
5

Nested type

Extensions can add new nested types to existing classes, structures, and enumerates:

extension Int {
   enum calc
   {
      case add
      case sub
      case mult
      case div
      case anything
   }

   var print: calc {
      switch self
      {
         case 0:
            return .add
         case 1:
            return .sub
         case 2:
            return .mult
         case 3:
            return .div
         default:
            return .anything
       }
   }
}

func result(numb: [Int]) {
   for i in numb {
      switch i.print {
         case .add:
            print(" 10 ")
          case .sub:
            print(" 20 ")
         case .mult:
         print(" 30 ")
         case .div:
         print(" 40 ")
         default:
         print(" 50 ")

      }
   }
}

result([0, 1, 2, 3, 4, 7])

The output of the above program execution is:

 10 
 20 
 30 
 40 
 50 
 50