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

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)

swift语言点评二十一-协议的更多相关文章

  1. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  2. swift语言点评二十-扩展

    结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...

  3. Swift语言指南(二)--语言基础之注释和分号

    原文:Swift语言指南(二)--语言基础之注释和分号 注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时会忽略注释. Swift的注释与C语言极其相似,单 ...

  4. Swift 学习之二十一:?和 !(详解)

    http://blog.csdn.net/woaifen3344/article/details/30244201 Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始 ...

  5. swift语言点评十二-Subscripts

    Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...

  6. swift语言点评十一-Methods

    Assigning to self Within a Mutating Method Mutating methods can assign an entirely new instance to t ...

  7. 苹果新的编程语言 Swift 语言进阶(十一)--实例的初始化与类的析构

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程.初始化包括设置一个实例的每一个存储属性为一个初始值,以及执行任何其它新的实例能够使用之前需要的设置或初始 ...

  8. Swift5 语言指南(二十一) 嵌套类型

    通常创建枚举以支持特定类或结构的功能.类似地,定义纯粹在更复杂类型的上下文中使用的实用程序类和结构可能是方便的.为此,Swift允许您定义嵌套类型,从而在它们支持的类型定义中嵌套支持枚举,类和结构. ...

  9. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

随机推荐

  1. BZOJ4819: [Sdoi2017]新生舞会(01分数规划)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1029  Solved: 528[Submit][Status][Discuss] Descripti ...

  2. 查看md文件

    使用命令将md文件转为html,在浏览器中演示 通过npm安装i5ting_toc 安装好node之后,可以直接使用npm.Windows+R打开运行框,输入cmd,打开命令窗口.连网的情况下,输入如 ...

  3. Android Studio配置GreenDAO 3.2.0和使用方法

    我相信,在平时的开发过程中,大家一定会或多或少地接触到SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写SQL语句与解析查询结果等.所以,适用于Android ORM框架也就孕育而生了 ...

  4. 搭建自己的koa+mysql后台模板

    1.在vscode里面打开一个文件夹 2.cnpm init 3.导入必要的依赖项 "dependencies": { "koa": "^2.7.0& ...

  5. 移动端和pc端的判断,不同端做不同的处理

    1.通过js判段是pc端还是移动端 function browserRedirect() { var type = ""; var sUserAgent = navigator.u ...

  6. post数据html数据获取危险处理办法

      基础小知识 ValidateRequest属性是Page类中比较常用的属性,用来指示是否对输入数据进行潜在危险性检查.在默认情况下为True,就是表示 “是对输入的数据进行潜在危险性检查”,这个属 ...

  7. 开放个人电脑端口[Windows]

    先打开控制面板

  8. 基于element的表单渲染器 (el-form-renderer)

    基于 element-ui 封装的表单渲染器,完整继承了 element 的属性定义,并进行了简单扩展,从而用户能够通过使用一段预设的数据渲染出一个完整的 element 表单. 演示地址 项目地址 ...

  9. 使用InstelliJ IDEA创建Spring MVC应用程序

    环境版本 Windows 8.1 IDE:InstelliJ IDEA 13    Spring:Spring 4.1.1 & Spring MVC 4.1.1    WebLogic 10. ...

  10. 常用的ES6方法

    常用的ES6方法 ES6之后,新增了定义变量的两个关键字,分别是let和const. let和const都能够声明块级作用域,用法和var是类似的,let的特点是不会变量提升,而是被锁在当前块中. 实 ...