定义有什么,及哪些必须实现。

protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

Property Requirements

The protocol doesn’t specify whether the property should be a stored property or a computed property—it only specifies the required property name and type.

  1. protocol SomeProtocol {
  2. var mustBeSettable: Int { get set }
  3. var doesNotNeedToBeSettable: Int { get }
  4. }

If you mark a protocol instance method requirement as mutating, you don’t need to write the mutatingkeyword when writing an implementation of that method for a class. The mutating keyword is only used by structures and enumerations.

Class-Only Protocols

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObjectprotocol to a protocol’s inheritance list.

  1. protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
  2. // class-only protocol definition goes here

协议联合体作为参量

  1. func wishHappyBirthday(to celebrator: Named & Aged) {
  2. print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
  3. }
  4. let birthdayPerson = Person(name: "Malcolm", age: 21)
  5. wishHappyBirthday(to: birthdayPerson)

最新文章

  1. Windows Server 2012 虚拟化实战:存储(二)
  2. phpcms调用一级栏目和二级栏目
  3. c# 结构体、枚举类型及函数调用
  4. 如何将后台传来的json反序列化为前端具体对象
  5. JSON 教程学习进度备忘
  6. HDU4453--Looploop (Splay伸展树)
  7. Find Peak Element 解答
  8. [问题解决] ubuntu server12.04 认证的问题
  9. experss框架—基础认识
  10. 【JAVAWEB学习笔记】03_JavaScript
  11. 聊聊keep-alive组件的使用及其实现原理
  12. Sony索尼数码录音笔MSV格式转换为MP3格式【转】
  13. 005_elasticsearch的数据导入导出
  14. SO_REUSEADDR SO_REUSEPORT
  15. Linux 防火墙firewalld
  16. tr,td高度不生效
  17. 汉诺塔X
  18. 关于git的基本使用
  19. jquery插件中(function ( $, window, document, undefined )的作用
  20. Java 正则表达式 Pattern & Matcher

热门文章

  1. Linux top命令简解
  2. windows下git server搭建
  3. ZBrush设计制作小怪兽并用KeyShot渲染
  4. idea--IntelliJ IDEA隐藏不想看到的文件或文件夹
  5. luoguP5055 【模板】可持久化文艺平衡树 可持久化非旋转treap
  6. Build rpm example:zram
  7. Chrome Is The New C Runtime
  8. Layui表格编辑【不依赖Layui的动态table加载】
  9. React:关于虚拟DOM(Virtual DOM)
  10. List<T>与List<?>的区别