Typealiastypealias is used to redefine the name of a type that already exists, and by naming it, you can make the code clearer. The syntax used is also simple, as with the Typealias keyword, you can assign a new name to a type that already exists, as with a normal assignment statement. For example, when calculating the distance and position on the two-dimensional plane, we usually use double to denote the distance and use Cgpoint to represent the position:
[JavaScript]View PlainCopy
- func distance (_ point: cgpoint, _ Anotherpoint: cgpoint) -> double {
- let dx = double (anotherpoint.x - point.x)
- let dy = double (ANOTHERPOINT.Y - POINT.Y)
- return sqrt (dx * dx + dy * dy)
- }  
-
- let origin = cgpoint (x: 3, y: 0)
- let point = cgpoint (x: 4, y: 0)
-   
- let d = distance ( Origin, point)
Although there is no problem with the math and the final program running, there is always something wrong with reading and maintenance. Because we do not combine mathematical abstractions with practical problems, we also need to make an extra transition in the brain when reading code: Cgpoint represents a point, and this point is where we are in the defined coordinate system; A double is a number that represents the distance between two points. If we use Typealias, we can write this transformation directly in the code, thus reducing the burden of reading and maintenance:
[JavaScript]View PlainCopy
- func distance (_ point: cgpoint, _ Anotherpoint: cgpoint) -> double {
- let dx = distance (anotherpoint.x - point.x)
- let dy = distance (ANOTHERPOINT.Y - POINT.Y)
- return sqrt (dx * dx + dy * dy)
- }  
-
- let origin = location (x: 3, y: 0)
- let point = location (x: 4, y: 0)
-   
- let d = distance ( Origin, point)
The same code, with the help of Typealias, is much easier to read. There may not be a lot of experience with this simple example, but when you're faced with more complex practical problems, you can stop worrying and think about what's in your code, like the piles of int or string primitives, that represent something, so you can save a lot of brain cells. Note: During the development process, the use of Typealias is often used when compared to the use of the package
[JavaScript]View PlainCopy
- Typealias Success = (_ data:string), Void
- Typealias Failure = (_ error:string), Void
- Func Request (_ Url:string, success:success, failure:failure) {
- //Do request here ....
- }
Typealias and generic typealias are single, meaning that you must specify that a particular type be assigned a new name by Typealias, and that the entire generic type cannot be renamed. The following names are not compiled:
[JavaScript]View PlainCopy
- Class Person<t> {}
- Typealias Woker = person
- Typealias Worker = person<t>
But if we also introduce generics in aliases, we can do the corresponding
[JavaScript]View PlainCopy
- Class Person<t> {}
- Typealias Woker = person
- Typealias worker<t> = person<t>
When the certainty of a generic type is guaranteed, it is clear that aliases are also available:
[JavaScript]View PlainCopy
- Class Person<t> {}
- Typealias WorkID = String
- Typealias Worker = person<workid>
Another usage scenario is when a type implements a combination of multiple protocols at the same time. We can use the & notation to connect several protocols, and then give them a new more contextual name to enhance the readability of the code:
[JavaScript]View PlainCopy
- Protocol Cat {}
- Protocol Dog {}
- Typealias Pat = Cat & Dog
Associatedtype When an association type defines a protocol, it can sometimes be useful to declare one or more association types as part of the protocol definition. An association type provides a placeholder (or alias) for a type in the protocol, and the actual type it represents is not specified until the protocol is accepted. You can specify the association type by associatedtype the keyword. For example, the method of updating a cell with a protocol declaration:
[JavaScript]View PlainCopy
- Model
- struct Model {
- Let Age:int
- }
- protocols, using association types
- Protocol Tableviewcell {
- Associatedtype T
- Func Updatecell (_ Data:t)
- }
- Compliance with Tableviewcell
- Class Mytableviewcell:uitableviewcell, Tableviewcell {
- Typealias T = Model
- Func Updatecell (_ Data:model) {
- //do something ...
- }
- }
Swift-Keywords (typealias, associatedtype)