博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift 泛型的类型约束
阅读量:6905 次
发布时间:2019-06-27

本文共 2386 字,大约阅读时间需要 7 分钟。

总结:

1、类型约束只能添加到泛型参量上面

2、关联类型是泛型参量;

3、关联类型可以通过 协议.关联类型名称的形式引用;

  • func allItemsMatch<C1: Container, C2: Container>
  •     (_ someContainer: C1, _ anotherContainer: C2) -> Bool
  •     where C1.Item == C2.Item, C1.Item: Equatable

4、约束的语法有两种:1)继承类语法 2)where语句语法;

继承类语法是约束语法的简化形式,适用于继承和符合语句的形式:

public struct Tuple<A: Equatable, B: Equatable>

直接把约束添加到类型参量的声明中;

 

where语句形式的约束有两重作用:

1)增加可读性;将约束从主声明中剥离;

2)添加非继承形式的约束和复杂的约束功能

where T == Rule.RowValueType

associatedtype Iterator: IteratorProtocol where Iterator.Element == Item

public extension Request where Response == Void

public extension Thenable where T: Sequence, T.Iterator.Element: Comparable

 

 

https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553

'where' clause cannot be attached to a non-generic declaration

Type Constraints

Type constraints specify that a type parameter must inherit from a specific class, or conform to a particular protocol or protocol composition.

 

For example, Swift’s Dictionary type places a limitation on the types that can be used as keys for a dictionary. As described in , the type of a dictionary’s keys must be hashable. That is, it must provide a way to make itself uniquely representable. Dictionary needs its keys to be hashable so that it can check whether it already contains a value for a particular key. Without this requirement, Dictionary could not tell whether it should insert or replace a value for a particular key, nor would it be able to find a value for a given key that is already in the dictionary.

 

public struct Dictionary<Key, Value> where Key : Hashable

 

  • protocol Container {
  •     associatedtype Item
  •     mutating func append(_ item: Item)
  •     var count: Int { get }
  •     subscript(i: Int) -> Item { get }
  •     associatedtype Iterator: IteratorProtocol where Iterator.Element == Item
  •     func makeIterator() -> Iterator
  • }

protocol ComparableContainer: Container where Item: Comparable { }

 

  • func allItemsMatch<C1: Container, C2: Container>
  •     (_ someContainer: C1, _ anotherContainer: C2) -> Bool
  •     where C1.Item == C2.Item, C1.Item: Equatable

 

 

  • func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
  •     // function body goes here
  • }

 

public struct Tuple<A: Equatable, B: Equatable> {

    

    public let a: A

    public let b: B

 

    public init(a: A, b: B) {

        self.a = a

        self.b = b

    }

 

}

protocol ComparableContainer: Container where Item: Comparable { }

 

protocol BatchedCollectionType: Collection {

    associatedtype Base: Collection

}

 

转载地址:http://apldl.baihongyu.com/

你可能感兴趣的文章
FastDFS蛋疼的集群和负载均衡(九)之创建FastDFS的Maven项目
查看>>
PHP 汉字 排序方法
查看>>
关于Hexo6.0搭建个人博客(github+Google-收录篇)
查看>>
如何使用Spring的FactoryBean接口
查看>>
10 - 页面侧边栏:使用自定义模板标签
查看>>
机器学习笔试题精选(三)
查看>>
课程二:创建和修改代码库
查看>>
浅尝flutter中的flex布局
查看>>
一张图彻底理解Javascript原型链
查看>>
Advanced-react-patterns(1)
查看>>
Android 布局中的空格以及占一个汉字宽度的空格的实现
查看>>
看完《寻梦环游记》,我抓爬了700多条影评
查看>>
前端特效【第04期】|果汁混合效果-下
查看>>
用爬虫写一个 GitHub Trending API
查看>>
程序员想搞机器学习?看看Nodejs之父这一年摸爬滚打的心路历程
查看>>
分布式Redis深度历险-Clustor
查看>>
为什么arrayList.removeAll(set)的速度远高于arrayList.removeAll(list)?
查看>>
iOS一个少代码侵入的滚动页码控件-CSFooterCircleFunctionView
查看>>
NLP系列学习:潜在语义牵引
查看>>
Swift 4官方文档中文版 Collection Types(上)
查看>>