HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
It is to extend an existing class, struct, or enumeration type to add new features.
Extensions can add new features to a type, but can not override existing functionality.
The Swift expansion can:
Extension declarations use the keywordextension:
extension SomeType { // 加到SomeType的新功能写到这里 }
An extension can extend an existing type, it can be adapted to one or more protocols, syntax is as follows:
extension SomeType: SomeProtocol, AnotherProctocol { // 协议实现写到这里 }
Extensions can add a calculated example of the type of property and computational properties to existing types.
The following example adds five properties to the calculation example of the type Int and extend 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 above program execution output is:
加法运算后的值:103 减法运算后的值:110 乘法运算后的值:390 除法运算后的值: 11 混合运算结果:154
Extensions can add new constructors to existing types.
This allows you to extend other types, your own custom type as a constructor argument, or the type of additional initialization options not included in the original implementation provided.
Extensions can add new convenience constructor to the class init (), but they can not add new designated 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 above program execution output is:
mult 模块内 (100, 200) mult 模块内 (200, 100) mult 模块内 (300, 500) mult 模块内 (300, 100) Sum 模块内:(100, 200) Diff 模块内: (200, 100)
Extensions can add new methods and instance type methods to existing types.
The following example adds a new instance method named topics to Int type:
extension Int { func topics(summation: () -> ()) { for _ in 0/en<self { summation() } } } 4.topics({ print("扩展模块内") }) 3.topics({ print("内型转换模块内") })
The above program execution output is:
扩展模块内 扩展模块内 扩展模块内 扩展模块内 内型转换模块内 内型转换模块内 内型转换模块内
The topics
method uses a () -> ()
single type parameter, indicating that the function has no parameters and no return value.
After defining the extension, you can call for any integer topics
methods to achieve the function is to perform a task repeatedly:
Examples of methods by adding extensions can also modify the instance itself.
Structure and enumerated types modify self or property methods must be marked as the instance method of mutating, just modify the original implementation of the method from the same.
The following example adds a new modification method called square to Swift's Double type to achieve a square original value calculation:
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 above program execution output is:
圆的面积为: 34.210935 圆的面积为: 105.68006 圆的面积为: 45464.070735
Extensions can add new index to an existing type.
The following example to Swift adds a built-in type Int integer index. The index [n] Returns the decimal 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 above program execution output is:
2 6 5
You can expand the existing classes, structures, and enumerations add a new nested types:
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 above program execution output is:
10 20 30 40 50 50