What does <> (angle brackets) do on class names

2020-02-10 14:23发布

问题:

In a class declaration what is the use of <> angle brackets and the parameters declared inside in swift? Like as:

public class ClassName<T: Comparable> {


}

回答1:

It makes the class generic. The Swift standard library doesn't have a lot of examples of generic classes, but it has some very well-known generic structs and enums:

public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer

public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible

public enum Optional<Wrapped> : _Reflectable, NilLiteralConvertible

Read more about generics under “Generics” in The Swift Programming Language.



标签: swift class