Swift下标

还记得字典吗?html


[html] view plaincopy在CODE上查看代码片派生到个人代码片app

  1. var numberOfLegs= ["spider": 8,"ant": 6, "cat":4]  ide

  2. numberOfLegs["bird"]= 2  ui



["bird"]就是下标 下标能够在类和结构体中定义。spa

定义下标.net


[html] view plaincopy在CODE上查看代码片派生到个人代码片code

  1. subscript(index: Int) -> Int {  orm

  2. get {  htm

  3. //return anappropriate subscript value here  blog

  4. }  

  5. set(newValue) {  

  6. // perform a suitable settingaction here  

  7. }  

  8. }  


 

 

只读下标


[html] view plaincopy在CODE上查看代码片派生到个人代码片

  1. subscript(index: Int) -> Int {  

  2.    

  3. //return anappropriate subscript value here  

  4.    

  5. }  

  6.    



实例:


[html] view plaincopy在CODE上查看代码片派生到个人代码片

  1. struct TimesTable {  

  2.    let multiplier: Int  

  3.     subscript(index: Int) ->Int {  

  4.          return multiplier * index  

  5.       }  

  6.  }  

  7.  let threeTimesTable = TimesTable(multiplier: 3)  

  8.  println("sixtimes three is \(threeTimesTable[6])")  

  9. // prints"six times three is 18"  



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

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

相关文章
相关标签/搜索