Swift 2.x 函数变化
jopen
9年前
之前写了一篇全局函数,现在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/