`

Swift协议(Protocol)

c 
阅读更多

协议是为方法、属性等定义一套规范,没有具体的实现。

协议能够被类、结构体等具体实现(或遵守)。

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. protocol SomeProtocol {  
  2.  // protocoldefinition goes here  
  3.  }  
  4.  struct         SomeStructure:            FirstProtocol, AnotherProtocol {  
  5. // structure definition goes here}  
  6. class  SomeClass:    SomeSuperclass,     FirstProtocol, AnotherProtocol {  
  7.  // class definitiongoeshere  
  8.  }  

 

 

 

属性

 

1. set 和 get 访问器

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  protocol SomeProtocol {  
  2.  var mustBeSettable:Int { get set }  
  3. var doesNotNeedToBeSettable: Int { get }  
  4.  }  

 

 

 

2.静态属性

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. protocol AnotherProtocol {  
  2.  class var someTypeProperty: Int { get set }  
  3.  }  

 

 

3.只读

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. protocol FullyNamed {  
  2. var fullName: String { get }  
  3.  }  

 

 实例:

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct Person: FullyNamed {  
  2.  varfullName: String  
  3.  }  
  4.  letjohnPerson(fullName: "John Appleseed")  
  5.  class Starship: FullyNamed {  
  6.  varprefix: String?  
  7.  varname: String  
  8. init(name: String, prefix: String? = nil) {  
  9.  self.name = name self.prefix = prefix  
  10. }  
  11.  varfullName: String {  
  12.  return (prefix ? prefix!+ " " :"")+ name  
  13.  }  
  14.  }  
  15.  varncc1701 = Starship(name: "Enterprise",prefix: "USS")  
  16.    

 

 

 方法

 1.定义方法

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  protocol RandomNumberGenerator{  
  2. func random() -> Double  
  3. }  

 

 

2.定义静态方法

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  protocolSomeProtocol {  
  2.  class func someTypeMethod()  
  3. }  

 

 

实例:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. protocol RandomNumberGenerator{  
  2.  funcrandom() -> Double  
  3.  }  
  4.  class                   LinearCongruentialGenerator:RandomNumberGenerator {  
  5. var lastRandom42.0let m = 139968.0  
  6. let a = 3877.0 let c = 29573.0  
  7. funcrandom() -> Double {  
  8.  lastRandom = ((lastRandom * a + c) %m)  
  9.  returnlastRandom / m  
  10.  }  
  11.  }  
  12.  let generatorLinearCongruentialGenerator()  
  13.  println("Here's       a        random         number:  
  14. \(generator.random())")  
  15.  //    prints    "Here's     a     random       number:0.37464991998171"  
  16.  println("And another one: \(generator.random())")  
  17.  //prints "And another one: 0.729023776863283"  

 

 

 把协议作为类型使用

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  protocol RandomNumberGenerator {  
  2.  func random() -> Double}  
  3.  class LinearCongruentialGenerator: RandomNumberGenerator {  
  4.  varlastRandom42.0 let m =139968.0  
  5. let a = 3877.0 letc = 29573.0  
  6. func random() -> Double {  
  7. lastRandom = ((lastRandom * a + c) %m)  
  8.  return lastRandom / m  
  9. }  
  10. }  
  11. class Dice {  
  12.  letsides: Int  
  13. let generator: RandomNumberGenerator  
  14.  init(sides: Int, generator: RandomNumberGenerator) {  
  15.  self.sides = sidesself.generator = generator  
  16. }  
  17. func roll() -> Int{  
  18. return Int(generator.random() * Double(sides)) + 1  
  19. }  
  20. }  
  21. vard6 = Dice(sides: 6, generator: LinearCongruentialGenerator())  
  22. for_ in 1...5 {  
  23. println("Randomdiceroll is \(d6.roll())")  
  24. }  

 

 

 

Swift交流讨论论坛论坛:http://www.cocoagame.net

欢迎加入Swift技术交流群:362298485

分享到:
评论

相关推荐

    让oc像swift那样给@protocol协议添加方法的默认实现教程加demo,

    让oc像swift那样给@protocol协议添加方法的默认实现教程加demo,例子里面实现了xib文件,遵守协议以后直接就能被调用的例子

    Swift中的协议(protocol)学习教程

    协议中可以定义一些基本的需要被实例化的属性,这里我们就来看一下Swift中的协议(protocol)学习教程,需要的朋友可以参考下

    协议Protocol.playground.zip

    Swift里的协议protocol练习

    protobuf-swift, 谷歌ProtocolBuffers的谷歌.zip

    protobuf-swift, 谷歌ProtocolBuffers的谷歌 用于Swift的协议缓冲器 Swift中协议缓冲区的实现。协议缓冲区是一种以高效但可扩展的格式编码结构化数据的方法。 这个项目是基于Google的协议缓冲区的实现。 有关更多...

    swift代码-用协议建立规则 protocol

    swift代码-用协议建立规则 protocol

    Protocol-Extension-Event-Dispatcher:使用Swift协议扩展实现EventDispatcher模式

    协议扩展事件调度程序使用Swift协议扩展实现EventDispatcher模式这是此博客文章的配套项目: : WWDC上Swift 2.0的一大亮点是引入了协议扩展:能够向协议中添加默认方法实现。 自WWDC以来,来自SketchyTech,David ...

    libsbp:Swift Binary Protocol客户端库

    Swift二进制协议的规范和绑定Swift导航二进制协议(SBP)是一种快速,简单且最小的二进制协议,用于与Swift设备通信。它是用来传输解决方案,观测值,状态和调试消息以及从主机操作系统接收消息(例如差分校正和历书...

    Protocol Oriented Programming (Swift 4)

    面向协议编程的教程,Swift3.0以后Apple提出的一种编程理念,充分发挥了Swift语言的特点,是进一步提高Swift水平不可多得的学习材料。高清英文版,目录齐全,而且可以自由编辑。

    Swift4.0源代码最新201802

    12.协议protocol.playground 13.类和结构体的区别.playground 14.枚举.playground 15.扩展.playground 16.下标.playground 17.泛型.playground 18.异常错误.playground 19.断言.playground 1初识.playground 20.自动...

    Protocol-Buffers-Swift-test:使用 Protocol Buffers 和 Swift 进行概念验证

    协议缓冲区 + Swift 使用 Protocol Buffers 和 Swift 进行概念验证 在制品 在制品 在制品

    ios-swift中简单使用UITableView swift与OC的混合开发.zip

    东西不多简单易懂、Swift语言如何使用UITableView、Swift与OC的混合开发、扩展(Extension)的简单使用、协议(protocol)

    Matrix:Matrix协议Swift包

    一个为制作的Swift包 SDK包含: Matrix , MatrixClient 。 如果您需要所有库,只需导入MatrixSDK 用法 矩阵 [矩阵]规范的全局实现。 MatrixClient 要开始使用[矩阵]客户端,您可以创建一个实例: let client = ...

    puppet-swift:OpenStack Swift Puppet模块

    :协议班级快速代理: 包括swift :: proxy 快速存储节点包括swift :: nodeSSL前端如果将swift_protocol设置为https,则swift代理将在端口8889上运行并在localhost上进行侦听,然后由您设置ssl代理。 参见nginx模块

    iOS Swift创建代理协议的多种方式示例

    本文主要给大家介绍了iOS Swift创建代理协议的各种方式,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 假如有一个类为 LXFView,现在要为这个类创建一个代理协议,我们该如何做呢? 首先,...

    grpc-swift:gRPC的Swift语言实现

    它旨在与Apple的对协议缓冲区的支持一起使用。 这两个项目都包含Google的Protocol Buffer编译器protoc代码生成插件,并且都包含构建和运行所生成的代码所需的支持代码库。 同时为gRPC客户端和服务器提供了API和生成...

    gRPC的Swift语言实现。-Swift开发

    它旨在与Apple的SwiftProtobuf对协议缓冲区的支持一起使用。 这两个项目都包含Google的Protocol Buffer编译器protoc的代码生成插件,并且都包含构建和运行所生成的代码所需的支持代码库。 gRPC Swift该存储库包含一...

    小码哥Swift5.0视频教程(非加密)

    1-22节课 1. 基本运算、流程控制、函数.mp4 2. 字符、字符串、枚举、集合、闭包】.mp4 3.... protocol、访问权限控制、反射.mp4 ... 21. 面向协议编程、响应式编程.mp4 22. 标准库源码分析、项目实战.

Global site tag (gtag.js) - Google Analytics