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

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. unwrap

    node.replaceWith(...node.childNodes);

  2. 【摘录】JAVA内存管理-有关垃圾收集的关键参数

    第八章 有关垃圾收集的关键参数 一些命令行参数可以用来选择垃圾收集器,指定堆或代的大小,修改垃圾收集行为,获取垃圾收集统计数据.本章给出一些最常用的参数.有关各种各样参数更多完整的列表和详细信息可以参 ...

  3. 最新消息,CDRX7冰点价再返现,你知道么?

    一年一度的七夕又到来了,这不很多的单身狗朋友们都已经自备好了狗粮,准备在家里宅上一天呢? 开个玩笑今天小编就为各位带来了 一个劲爆大消息... Deng/deng/deng/deng..就是备受万众瞩 ...

  4. 构造器参数过多时考虑使用构建器(Builder)

    一.静态工厂和构造器的局限性 面对需要大量可选参数才能构建对象时,静态工厂和构造器并不能随着可选参数的增加而合理扩展. 假设创建一个类Person需要使用大量的可选参数,其中两个参数是必填的,剩下的都 ...

  5. 连连看 HDU - 1175_搜索_剪枝

    hdu有毒,考试上 AC 的就是一直 WA- 其实这道题是可以进行初始化来进行优化的,这样的话询问次数是可以达到 10510^5105 的.不过普通的 dfsdfsdfs + 剪枝也是可过的. Cod ...

  6. SASS概览

    1.安装: sass需要使用ruby,首先安装ruby,之后: gem install sass 编译: sass input.scss output.css 2.快速入门: 变量: .scss 变量 ...

  7. debian 9 安装Virtual Box

    1.去官网下载deb包,例如包名: virtualbox-.2_5.2.18-124319_Debian_stretch_amd64.deb 2.安装 .2_5.2.18-124319_Debian_ ...

  8. Shell(二)运算符

    基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 ...

  9. class的基本操作方法

    JavaScript语言中,生成实例对象的传统方法是通过构造函数 function Point(x,y){ this.x = x; this.y = y; } Point.prototype.toSt ...

  10. Methods and systems for sharing common job information

    Apparatus and methods are provided for utilizing a plurality of processing units. A method comprises ...