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

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. PHP关于注册注意的问题

    1.注意转义字符的问题 get_magic_quotes_gpc()开启时,所有的 ' (单引号), " (双引号), \(反斜线) and 空字符(null)会自动转为含有反斜线的溢出字符 ...

  2. git + grunt 环境配置

        1⃣️ssh key生成步骤 一.设置Git的user.name和user.email: $ git config --global user.name "xiongzuyan&qu ...

  3. bzoj 3028: 食物 生成函数_麦克劳林展开

    不管怎么求似乎都不太好求,我们试试生成函数.这个东西好神奇.生成函数的精华是两个生成函数相乘,对应 $x^{i}$ 前的系数表示取 $i$ 个时的方案数. 有时候,我们会将函数按等比数列求和公式进行压 ...

  4. 路飞学城Python-Day21(practise)

    编写程序, 如下有三点要求: 自定义用户信息数据结构, 写入文件, 然后读取出内容, 利用json模块进行数据的序列化和反序列化 e.g { "egon":{"passw ...

  5. SVG矢量动画

    一.概述 相较于png.jpg等位图通过存储像素点来记录图像,svg (Scalable Vector Graphics)拥有一套自己的语法,通过描述的形式来记录图形.Android并不直接使用原始的 ...

  6. systemd bug: bz1437114 core:execute: fix fork() fail handling in exec_spawn()

    问题现象 大量僵尸进程 root 32278 0.0 0.0 0 0 ? Z 05:39 0:00 [runuser] <defunct> root 32280 0.0 0.0 0 0 ? ...

  7. 大数据相关文档&Api下载

    IT相关文档&Api下载(不断更新中) 下载地址:https://download.csdn.net/user/qq_42797237/uploads 如有没有你需要的API,可和我留言,留下 ...

  8. STM32 SPI 发送第一个数据不成功问题

    STM32的标准库,跟HAL库都是很实用的, 在使用SPI库的过程中一定要注意时序的问题. 我在调试SPI过程中,调试了两个IC,都是用HAL库, 第一个IC没出问题,第二个IC出现了第一次发送数据不 ...

  9. Bate版总结会议2

    本次会议主要是针对我们在冲刺阶段出现的问题进行的讨论.再有就是以后在开发中应该改进的地方. 问题一: 工作任务不能拖:因为任务一拖就很可能无法再规定的时间内完成,如果我们可以分配好任务就去做的话,我们 ...

  10. Java基础学习总结(57)——Jrebel插件热部署

    JavaEE开发环境下,Tomcat对热布署的支持还是比较弱,致使开发过程中浪费大量时间在重启服务上.胖先生讨厌来来回回的折腾,所以想看看有没有实时的编译,发现Jrebel的插件付费软件,它对热布署的 ...