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

2020-02-10 14:29发布

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> {


}

标签: swift class
1条回答
太酷不给撩
2楼-- · 2020-02-10 14:53

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.

查看更多
登录 后发表回答