Swift provides generics that allow you to write flexible and reusable functions and types.
The Swift standard library is built from generic code.
The array and dictionary types of Swift are generic sets.
You can create one.
Int
array, or you can create a
String
array, or even an array of type data of any other Swift.
The following example is a non-generic function The output of the above program execution is as follows: The above example only tries and swaps variables of type Int. If you want toexchange two Judging from the above code, the functional code is the same, but the type is different, so we can use generics to avoid repetitive coding. Generics use placeholder type names (represented here by the letter T) instead of actual type names (for example, The following example is a generic function The output of the above program execution is as follows: Swift allows you to define your own generic types. Custom classes, structures, and enumerations act on any type, as Next, let’s write a program called The image is parsed from left to right as follows: Three values are in the stack. The fourth value is pushed to the top of the stack. There are now four values in the stack, with the most recent value at the top. The top value in the stack is removed, or called off the stack. After removing a value, the stack now has only three values. The following example is a non-generic version of the stack to This structure uses a file named Above. Here is a generic version of the same code: The execution result of the instance is as follows: In the above example Create Designation Designation When you extend a generic type (using the The following example extends generic types
exchange
in exchange for two.
Int
Value: 9.44.1. Example #
//
Define a function funcswapTwoInts (\ a: inoutInt, \ _b: inoutInt) {lettemporaryA=aa=bb=temporaryA} varnumb1=100varnumb2=200print to exchange two variables
SwapTwoInts (&numb1,&numb2) print() after swapping data:
And \ \ (numb2)
Data before exchange: 100 and 200
Exchange data: 200 and 100
String
value or
Double
value, you have to rewrite the corresponding function, such as
swapTwoStrings(_:_:)
and
swapTwoDoubles(_:_:)
, as follows: 9.44.2.
String
and
Double
value exchange function # funcswapTwoStrings(\_a:inoutString,\_b:inoutString){lettemporaryA=aa=bb=temporaryA}funcswapTwoDoubles(\_a:inoutDouble,\_b:inoutDouble){lettemporaryA=aa=bb=temporaryA}
Int
、
String
or
Double
).func swapTwoValues<T>(_ a: inout T, _ b: inout T)
swapTwoValues
this is followed by the placeholder type name (T) and enclosed in angle brackets (
<T>
). This angle bracket tells Swift which T is.
swapTwoValues(_:_:)
a placeholder type name within the function definition, so Swift does not look for the actual type named T.
exchange
in exchange for two.
Int
and
String
value: 9.44.3. Example #
//
Define a function funcswapTwoValues<T>(\ a: inoutT, \ _b: inoutT) {lettemporaryA=aa=bb=temporaryA} varnumb1=100varnumb2=200print ("Data before exchange: \ \ (numb1)")
SwapTwoValues (&numb1,&numb2) print() after swapping:
Before exchanging data with \ \ (numb2) ") varstr1=" A "varstr2=" B "print(): \ \ (str1)
SwapTwoValues (&str1,&str2) print() after swapping:
And \ \ (str2)
Data before exchange: 100 and 200
Exchange data: 200 and 100
Data before exchange: A and B
Exchange data: B and A
9.44.4. Generic type #
Array
and
Dictionary
usage.
Stack
the generic collection type of(stack), the stack only allows new elements to be added at the end of the collection (called on the stack), and can only remove elements from the end (called off the stack).
Int
the stack of type An is an example: 9.44.5. Int type stack #
structIntStack{varitems=[Int]()mutatingfuncpush(\_item:Int){items.append(item)}mutatingfuncpop()->Int{returnitems.removeLast()}}
items
of
Array
property to store the value.
Stack
provide two methods:
push(_:
)
and
pop()
used to press values into and remove values from the stack These methods are marked as
mutating
. Because they need to modify the structure s
items
array.
IntStack
structures can only be used for
Int
type. However,you can define a generic type
Stack
structure so that any type of value can be processed. 9.44.6. Stack of generics #
structStack<Element>{varitems=[Element]()mutatingfuncpush(\_item:Element)
{items.append(item)}mutatingfuncpop()->Element{returnitems.removeLast()}}
varstackOfStrings=Stack<String>()print("String element push:")
stackOfStrings.push("google")stackOfStrings.push("runoob")print
(stackOfStrings.items);letdeletetos=stackOfStrings.pop()print
("Stack element:"+deletetos)varstackOfInts=Stack<Int>()print("Pushing integer elements onto the stack:")
stackOfInts.push(1)stackOfInts.push(2)print(stackOfInts.items);
String element push:
["google", "runoob"]
Stack element: runoob
Pushing integer elements onto the stack:
[1, 2]
Stack
basically and
IntStack
are the same, placeholder type parameter
Element
instead of the actual
Int
type.
Element
is used as placeholders in the following three places:
items
Property, use in
Element
initializes it with an empty array of type.
push(_:)
the unique parameter of the method
item
the type of must be
Element
type.
pop()
the return type of the method must be
Element
type. 9.44.7. Extended generic type #
extension
keyword), you do notneed to provide a list of type parameters in the extended definition. More conveniently, the list of type parameters declared in the original type definition can be used in the extension, and these parameter names from the original type are used as references to the type parameters in the original definition.
Stack
to which you added a file named
topItem
, which returns the element at the top of the current stack without removing it from the stack