Swift 2.x 函數變化

jopen 9年前發布 | 40K 次閱讀 Swift Apple Swift開發

之前寫了一篇全局函數,現在Swift2.1后,使用方法大變樣-_-|||
示例代碼:
https://github.com/conanwhf/swiftplayground/blob/master/Swift_StandardFunc.playground/Contents.swift

!!!
String不再是一個集合序列,所有相關調用需要改為String.chatacters
e.g. find(String, X)-> String.characters.indexOf(X)

func(obj, optional)->obj.func(optional)

  • enumerate()
  • dropFirst()
  • dropLast()
  • contains() (不適用于String)
  • indices()
  • maxElement()
  • minElement()
  • startsWith()
  • underestimateCount()

func(obj, optional)->obj.newfunc(optional)

  • advance(obj,x)-> obj.advancedBy(x)
  • distance(obj,x)-> obj.distanceTo(x)
  • contanins(String,str)->String.contaninsString(str)
  • equalElements(seq1,seq2)-> seq1.elementsEqual(seq2)
  • find(seq,x)-> seq.indexOf(x) (不適用于String)
  • join(seq,x)-> seq.joinWithSeparator(x)

rename/merged

  • toString(n)->String(n)
  • *println()->*print()
  • count(obj)-> obj.enumerate()

other

  • count(obj)-> obj.count
  • reduce(seq, init, combineClosure)-> seq.reduce(init, combine:combineClosure)
  • reflect(obj)-> Mirror(reflecting: obj)
  • reverse(arr)-> Array(reverse())
  • 一些條件序列操作函數由produce變成了function,即不修改序列本身,而是返回一個新的序列:func(obj,{rule})-> newobj = obj.func({rule}),包括filter(), map(), flatMap()等

來自:http://conanwhf.gitcafe.io/2015/11/02/Swift 2.x Function/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!