1. Functions with generics as parameters
Generics are the powerful core of the Swift language. Generics are abstractions of types. Using generic developers can express code intentions more flexibly and conveniently. We know that parameters with parameter functions must have a clear parameter type. Sometimes developers will encounter such a situation. Write a function to exchange the values of two variables. Since variables are of type, implementing the same function may require overloading into multiple functions to implement, which greatly wastes development costs. Using generics can perfectly solve this problem. The example code is as follows:
func exchange<T>(inout param1:T,inout param2:T){ let tmp = param1 param1 = param2 param2 = tmp } var p1 = "15" var p2 = "40" exchange(&p1, param2: &p2)
The above method can realize the exchange of any variable of the same type. Use generics in function parameters. You need to define parameter placeholders in <> after the function name. If there are multiple parameter placeholders, separate them with commas.
2. Application of generics in types
In addition to being a parameter and return value of a function, generics can also solve many very difficult problems when defining types, such as implementing a set type of a stack structure. The example code is as follows:
struct Stack<ItemType> { var items:[ItemType] = [] mutating func push(param:ItemType) { (param) } mutating func pop()->ItemType{ return () } } //Integer Stackvar obj1 = Stack<Int>() (1) () //String stackvar obj2 = Stack<String>() ("HS") ()
When extending a type that uses generics, you do not need to use <> to define generics, just use the original defined generic placeholder. The example is as follows:
extension Stack{ func getArray() -> [ItemType] { return items } }
Sometimes, developers need to impose some constraints on generics, such as only allowing this generic to inherit from a certain class or implement a certain protocol. The example code is as follows:
class MyClass { } //Only MyClass subclass can create Stack stackstruct Stack<ItemType:MyClass> { var items:[ItemType] = [] mutating func push(param:ItemType) { (param) } mutating func pop()->ItemType{ return () } }
In the protocol, another method can be used for generic programming, and type association can be performed using associated type keywords. Examples are as follows:
protocol MyProtocol { //Specify the type when implementing the protocol associatedtype ItemType var param:ItemType {get set} } class MyClass:MyProtocol { //Because Swift can automatically recognize types. This is the ItemType in MyProtocol. var param: Int = 0 }
3. The combination of generics and where clauses
Using the where clause can impose stricter constraints on generics to make them meet the logic needed by developers. The examples are as follows:
//T and C must comply with the integer protocolclass MyClassTwo<T,C where T:IntegerType,C:IntegerType> { var param1:T var param2:C init(param1:T,param2:C){ self.param1=param1 self.param2=param2 } } var obj3 = MyClassTwo(param1: 1, param2: 1)