Swift中用到extension的一些基本的擴充功能講解_Swift

來源:互聯網
上載者:User

現有的類,結構或枚舉類型的功能可以在擴充的協助下加入。類型的功能可以使用擴充加入,但重寫的功能不能使用擴充。

Swift 擴充功能:

  • 添加計算屬性並計算類型屬性
  • 定義執行個體和類型的方法
  • 提供了新的初始化
  • 定義下標
  • 定義和使用新的巢狀型別
  • 使現有類型符合協議

擴充用關鍵字 extension 聲明

文法

複製代碼 代碼如下:

extension SomeType {
   // new functionality can be added here
}
現有類型也可以用擴充加入使它作為一個協議標準和其文法類似於類或結構。

extension SomeType: SomeProtocol, AnotherProtocol {
   // protocol requirements is described here
}


計算屬性
計算“執行個體”和“type”屬性也可以擴充在擴充的協助下實現。
複製代碼 代碼如下:

extension Int {
   var add: Int {return self + 100 }
   var sub: Int { return self - 10 }
   var mul: Int { return self * 10 }
   var div: Int { return self / 5 }
}
   
let addition = 3.add
println("Addition is \(addition)")
   
let subtraction = 120.sub
println("Subtraction is \(subtraction)")
   
let multiplication = 39.mul
println("Multiplication is \(multiplication)")
   
let division = 55.div
println("Division is \(division)")

let mix = 30.add + 34.sub
println("Mixed Type is \(mix)")


當我們使用 playground 運行上面的程式,得到以下結果。

Addition is 103Subtraction is 110Multiplication is 390Division is 11Mixed Type is 154

初始化器
Swift 能夠靈活地通過擴充新的初始化添加到現有的類型。使用者可以添加自己的自訂類型來擴充已定義的類型,額外的初始化選項也是可以的。 擴充僅支援 init(). 而 deinit() 不被擴充支援。

複製代碼 代碼如下:

struct sum {
   var num1 = 100, num2 = 200
}
  
struct diff {
   var no1 = 200, no2 = 100
}
  
struct mult {
   var a = sum()
   var b = diff()
}
  
let calc = mult()
println ("Inside mult block \(calc.a.num1, calc.a.num2)")
println("Inside mult block \(calc.b.no1, calc.b.no2)")
  
let memcalc = mult(a: sum(num1: 300, num2: 500),b: diff(no1: 300, no2: 100))
 
println("Inside mult block \(memcalc.a.num1, memcalc.a.num2)")
println("Inside mult block \(memcalc.b.no1, memcalc.b.no2)")
  
extension mult {
   init(x: sum, y: diff) {
      let X = x.num1 + x.num2
      let Y = y.no1 + y.no2
   }
}
  

let a = sum(num1: 100, num2: 200)
println("Inside Sum Block:\( a.num1, a.num2)")
  
  
let b = diff(no1: 200, no2: 100)
println("Inside Diff Block: \(b.no1, b.no2)")


當我們使用 playground 運行上面的程式,得到以下結果。

Inside mult block (100, 200)Inside mult block (200, 100)Inside mult block (300, 500)Inside mult block (300, 100)Inside Sum Block:(100, 200)Inside Diff Block: (200, 100)

方法
新執行個體方法和類型的方法,可以在擴充的協助下進一步加入到子類。

複製代碼 代碼如下:

extension Int {
   func topics(summation: () -> ()) {
      for _ in 0..<self {
         summation()
      }
   }

4.topics({
   println("Inside Extensions Block")      
})   
   
3.topics({
   println("Inside Type Casting Block")      
}) 


當我們使用 playground 運行上面的程式,得到以下結果。

Inside Extensions BlockInside Extensions BlockInside Extensions BlockInside Extensions BlockInside Type Casting BlockInside Type Casting BlockInside Type Casting Block

topics() 函數使用參數是 (summation: () -> ()) 的類型表示該函數不帶任何參數 而且它不會返回任何值。調用該函數多次, 塊被初始化,並調用 topic()方法初始化。

不同執行個體方法變形
執行個體方法時也可以作為擴充聲明的變形。

修改自身的結構和計數的方法或它的屬性必須標註執行個體方法變形,就像是從一個原始的實現變形的方法。

複製代碼 代碼如下:

extension Double {
   mutating func square() {
      let pi = 3.1415
      self = pi * self * self
   }
}

var Trial1 = 3.3
Trial1.square()
println("Area of circle is: \(Trial1)")


var Trial2 = 5.8
Trial2.square()
println("Area of circle is: \(Trial2)")


var Trial3 = 120.3
Trial3.square()
println("Area of circle is: \(Trial3)")


當我們使用 playground 運行上面的程式,得到以下結果。

Area of circle is: 34.210935Area of circle is: 105.68006Area of circle is: 45464.070735

下標
添加新標已聲明執行個體也可以擴充。

複製代碼 代碼如下:

extension Int {
   subscript(var multtable: Int) -> Int {
      var no1 = 1
      while multtable > 0 {
         no1 *= 10
         --multtable
      }
      return (self / no1) % 10
   }
}
   
println(12[0])
println(7869[1])
println(786543[2])

當我們使用 playground 運行上面的程式,得到以下結果。

265

巢狀型別
巢狀型別為類,結構和枚舉執行個體,也可在擴充的協助下進行擴充。

複製代碼 代碼如下:

 extension Int {
   enum calc
   {
      case add
      case sub
      case mult
      case div
      case anything
   }

   var print: calc {
      switch self
      {
         case 0:
            return .add
         case 1:
            return .sub
         case 2:
            return .mult
         case 3:
            return .div
         default:
            return .anything
       }
   }
}

func result(numb: [Int]) {
   for i in numb {
      switch i.print {
         case .add:
            println(" 10 ")
          case .sub:
            println(" 20 ")
         case .mult:
         println(" 30 ")
         case .div:
         println(" 40 ")
         default:
         println(" 50 ")

      }
   }
}

result([0, 1, 2, 3, 4, 7])


當我們使用 playground 運行上面的程式,得到以下結果。

10 20 30 40 50 50 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.