Swift - 关键字(typealias、associatedtype)
Typealias
- func distance(_ point: CGPoint, _ anotherPoint: CGPoint) -> Double {
- let dx = Double(anotherPoint.x - point.x)
- let dy = Double(anotherPoint.y - point.y)
- return sqrt(dx * dx + dy * dy)
- }
- let origin = CGPoint(x: 3, y: 0)
- let point = CGPoint(x: 4, y: 0)
- let d = distance(origin, point)
- func distance(_ point: CGPoint, _ anotherPoint: CGPoint) -> Double {
- let dx = Distance(anotherPoint.x - point.x)
- let dy = Distance(anotherPoint.y - point.y)
- return sqrt(dx * dx + dy * dy)
- }
- let origin = Location(x: 3, y: 0)
- let point = Location(x: 4, y: 0)
- let d = distance(origin, point)
- typealias Success = (_ data: String) -> Void
- typealias Failure = (_ error: String) -> Void
- func request(_ url: String, success: Success, failure: Failure) {
- // do request here ....
- }
typealias与泛型
- class Person<T> {}
- typealias Woker = Person
- typealias Worker = Person<T>
- class Person<T> {}
- typealias Woker = Person
- typealias Worker<T> = Person<T>
- class Person<T> {}
- typealias WorkId = String
- typealias Worker = Person<WorkId>
- protocol Cat {}
- protocol Dog {}
- typealias Pat = Cat & Dog
associatedtype关联类型
- //模型
- struct Model {
- let age: Int
- }
- //协议,使用关联类型
- protocol TableViewCell {
- associatedtype T
- func updateCell(_ data: T)
- }
- //遵守TableViewCell
- class MyTableViewCell: UITableViewCell, TableViewCell {
- typealias T = Model
- func updateCell(_ data: Model) {
- // do something ...
- }
- }
Swift - 关键字(typealias、associatedtype)的更多相关文章
- iOS开发——Swift篇&Swift关键字详细介绍
Swift关键字详细介绍 每一种语言都有相应的关键词,每个关键词都有他独特的作用,来看看swfit中的关键词: 关键词: 用来声明的: “ class, deinit, enum, extension ...
- Swift typealias associatedType
使用typealias为常用数据类型起一个别名, 一方面更容易通过别名理解该类型的用途, 另一方面还可以减少日常开发的代码量. typealias使用实例: // 网络请求常用回调闭包 typeali ...
- Swift 关键字汇总
常见的关键字有以下4种 与声明有关的关键字:class.deinit.enum.extension.func.import.init.let.protocol.static.struct.subscr ...
- Swift 关键字 inout - 让值类型以引用方式传递
两种参数传递方式 值类型 传递的是参数的一个副本,这样在调用参数的过程中不会影响原始数据. 引用类型 把参数本身引用(内存地址)传递过去,在调用的过程会影响原始数据. 在 Swift 众多数据类型中, ...
- [Swift]关键字:Self、self与super
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- swift 关键字willSet 和 didSet
// 下面是苹果给出的解释,就是在给属性设置新值的时候,可以在设置前和设置后做一些处理,这两个关键字就好像对该属性变化的监控 If you don’t need to compute the prop ...
- Swift类型转换 和 类型别名的定义(typealias)
(一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...
- 一览Swift中的常用关键字
要学习Swift这门语言,就必须先了解Swift的关键字及对应的解释.这里就列一下在Swift中常用到的关键字. 关键字是类似于标识符的保留字符序列,除非用重音符号(`)将其括起来,否则不能用作标识符 ...
- Swift 2.2 的新特性
导读:本文来自SwiftGG翻译组,作者@walkingway基于苹果Swift官方博客中Ted Kremenek所撰写的"Swift 2.2 Released!"文章进行了关于S ...
随机推荐
- Xcache3.2.0不支持php7.0.11
编译安装xcache3.2.0时在make这一步报错: AUTOCHECK missing : "arg_flags" "cache_size" AUTOCHE ...
- 渗透实战(周一):VMmare Fusion安装Kali Linux和win7虚拟机
高配笔记本电脑i5/8G/265G .VMware Fusion .Kali Linux镜像 .Win7镜像 第一:下载软件 1.如果你是苹果系统,建议下载最新VMware Fusion 11.0. ...
- android 下载网络图片并缓存
异步下载网络图片,并提供是否缓存至内存或外部文件的功能 异步加载类AsyncImageLoader public void downloadImage(final String url, final ...
- c#打包时 Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin\Debug\Emgu.CV.DebuggerVisualizers.VS2012.dll" ISEXP : error : -6103: Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin
1.错误:C#打包时发生如下错误: 错误 1 -6103: Could not find file "I:\VS2012\myWork\SmartCam\SmartCam\bin\Debug ...
- hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
#include<stdio.h> #include<string.h> #define N 21000 struct node { int v,next; }bian[510 ...
- mongodb--分片架构【待填的坑】
首先有一个问题没有搞懂:什么是自动分片?用脚本吗? 一: 多机方式中的另一种方式[分片 => sharding] 分片的对象的谁? 对一个[集合 => 表]进行拆分,把一个大数据拆分成多个 ...
- eclipse重置页面恢复到最初布局状态
eclipse重置页面恢复到最初布局状态 window->perspective->reset perspective
- sendfile学习
参考 https://zhuanlan.zhihu.com/p/20768200?refer=auxten 而成本很多时候的体现就是对计算资源的消耗,其中最重要的一个资源就是CPU资源. Sendfi ...
- HDU 3007
基本小圆覆盖模板题 #include <iostream> #include <algorithm> #include <cmath> using namespac ...
- HTML5的未来
2014年10月29日,万维网联盟(W3C)宣布,经过差点儿8年的艰辛努力.该标准规范终于终于制定完毕.之所以是8年,由于在1999年HTML4的规范制定以后,W3C对于HTML的发展.貌似就不再那么 ...