• Swift协议(Protocol)


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

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

    [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.  letjohn= Person(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 lastRandom= 42.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 generator= LinearCongruentialGenerator()  
    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.  varlastRandom= 42.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

  • 相关阅读:
    【编程开发】加密算法(MD5,RSA,DES)的解析
    【spring-quartz】 定时调度,时间设置
    IntelliJ IDEA 学习(六)内存设置
    Java中的Double类型计算
    货币金额的计算
    关系数据库设计三大范式【转】
    【服务器防护】WEB防护
    遮罩、警告框/弹框
    Java 中使用 UEditor 整理【待续。。。】
    order by 容易出现的bug记录
  • 原文地址:https://www.cnblogs.com/iOS-Blog/p/3831269.html
Copyright © 2020-2023  润新知