| Start |
|---|
| The Basics of Generics Generics (opens new window) are placeholders for types, allowing you to write flexible code that can be applied across multiple types. The advantage of using generics over A generic placeholder is defined within angle brackets Generic FunctionsFor functions (opens new window), this placeholder is placed after the function name: In this case, the generic placeholder is Here we’re passing two integers to the function. Therefore Swift is inferring Because of the strong type safety that generics offer – both the arguments and return of the function must be the same type. Therefore the following will not compile: Generic TypesIn order to use generics with classes (opens new window), structs (opens new window) or enums (opens new window), you can define the generic placeholder after the type name. This generic placeholder will require a type when you come to use the class Here the generic placeholder When used with a type, the given generic placeholder will keep its type for the entire lifetime of the given instance, and cannot be changed after initialisation. Therefore when you access the property Passing Around Generic TypesWhen you come to pass around generic types, in most cases you have to be explicit about the generic placeholder type you expect. For example, as a function input: This function will only accept a Generic Placeholder NamingGeneric placeholder names are not just limited to single letters. If a given placeholder represents a meaningful concept, you should give it a descriptive name. For example, Swift’s |
| Constraining Generic Placeholder Types It is possible to force the type parameters of a generic class to implement a protocol (opens new window), for example, Whenever we create a new |
| Generic Class Examples A generic class with the type parameter We can now create new objects using a type parameter Generics can also be created with multiple type parameters And used in the same way |
| Using Generics to Simplify Array Functions A function that extends the functionality of the array by creating an object oriented remove function. Usage Use the function to remove an element without need for an index. Just pass the object to remove. |
| Generic Class Inheritance Generic classes can be inherited: |
| Use generics to enhance type-safety Let's take this example without using generics The protocol declaration seems fine unless you actually use it. Why do you have to cast the result to By using generics you can avoid this problem that can cause runtime errors (and we don't want to have them!) |
| Advanced Type Constraints It's possible to specify several type constraints for generics using the It's also valid to write the Extensions can be restricted to types that satisfy conditions. The function is only available to instances which satisfy the type conditions: RemarksGeneric code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. For example, Swift's |
Read Article
https://develop.horje.com/learn/1434/reference