swift 常用库函数

     //countElements 入参为<T : _CollectionType>(x: T) 返回值T.Index.Distance 本demo返回11

       println(countElements("hello swift"))//11

      

        //0:A 1:B 2:C 返回一个新的序列,其中每个元素是一个元组,第一个值为原来元素所在的位置`index`,第二个为原来序列中的元素

        for (i,j) in enumerate(["A","B","C"])

        {

            println("\(i):\(j)")

        }

        

        //返回数字的绝对值

        println(abs(-100))//100

        println(abs(565))//565

        

       // contains(sequence, element):如果某个序列sequence(比如说一个数组)包含指定的元素element ,则返回true,否则返回false

        println(contains(["Swift","Objective-C"], "Swift"))//true

        println(contains(["Swift","Objective-C"], "Jave"))//false

        

        //dropFirst(sequence):返回一个去掉了首个元素的、新的序列(比如一个新数组)。

        println(dropFirst(["Swift","Objective-C","Java","C#"]))//[Objective-C, Java, C#]

        

        //dropLast(sequence):返回一个去掉了最后一个元素的、新的序列(比如一个新数组)

         println(dropLast(["Swift","Objective-C","Java","C#"]))//[Swift, Objective-C, Java]

        

        //dump(object):打印出某个对象object的所有信息 包括元素个数,

//        4 elements

//        - [0]: Swift

//        - [1]: Objective-C

//        - [2]: Java

//        - [3]: C#

//        [Swift, Objective-C, Java, C#]

        println(dump((["Swift","Objective-C","Java","C#"])))

        

      //equal(sequence1, sequence2):判断两个序列是否相等

       println(equal(["Swift","Objective-C","Java","C#"], ["Swift","Objective-C","Java","C#"]))//true

       println(equal(["Swift","Objective-C","Java","C#"], ["Swift","Objective-C","Java","C#","Javascript"]))//false

   

        //filter(sequence, includeElementClosure):对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包结果为true的元素合成一个新序列sequence并返回

        for i in filter(1...100, {$0%10 == 1}){

            println(i)//1,11,21,31,41,51,61,71,81,91

        }

        println(filter(1...100, {$0%10 == 1}))//[1, 11, 21, 31, 41, 51, 61, 71, 81, 91] 区别于map函数 

 

        //find(sequence, element):返回序列sequence中某元素element的位置index。如果序列中不存在此元素,则返回nil

        println(find(["Swift","Objective-C","Java","C#","Javascript"], "Java"))//Optional(2)

        println(find(["Swift","Objective-C","Java","C#","Javascript"], "C++"))//nil

        

        //indices(sequence):返回序列sequence中所有元素的位置(indices是index的复数)

        println(indices(["Swift","Objective-C","Java","C#","Javascript"]))//0..<5

        

        //join(separator, sequence):将序列sequence通过分隔符separator连成一个字符串,并返回此字符串

        var lang = join(":", ["Swift","Objective-C","Java","C#","Javascript"])

        println(lang)//Swift:Objective-C:Java:C#:Javascript

        

        //map(sequence, transformClosure):对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包的结果合成一个新序列sequence并返回 区别于filter返回的是把有为true结果的元素序列

        println(map(1...10, {$0%3 == 1}))//[true, false, false, true, false, false, true, false, false, true]

        

        //maxElement(sequence):返回序列sequence中的最大值。

        println(maxElement(1...100))//100

        println(maxElement(["Swift","Objective-C","Java","C#","Javascript"]))//Swift 这里有个疑问 为什么数组放的是数字的时候会报错

        

        //minElements(sequence):返回序列sequence中的最小值

        println(minElement(["Swift","Objective-C","Java","C#","Javascript"]))//C#

        

        //reduce(sequence, initial, combineClosure):给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值

        println(reduce(["Swift","Objective-C","Java","C#","Javascript"], "/", {$0 + $1}))// /SwiftObjective-CJavaC#Javascript

        println(reduce([10,20,5,3], 1, {$0 + $1}))//39

        

        //reverse(sequence):返回逆序的序列sequence

        println(reverse(["Swift","Objective-C","Java","C#","Javascript"]))//[Javascript, C#, Java, Objective-C, Swift]

        

        //startsWith(sequence1, sequence2):如果序列sequence1中开头的元素跟序列sequence2中的所有元素都相等,则返回true,否则返回false 也就是序列二中的元素是序列一中元素开头起的子集

        println(startsWith("helloswift", "hello"))//true

         println(startsWith("helloswift", "hellp"))//false

        println(startsWith(["Swift","Objective-C","Java","C#","Javascript"],["Swift","Objective-C","Java"]))//true

        println(startsWith(["Swift","Objective-C","Java","C#","Javascript"],["Swift","Objective-C","Java","C++"]))//false

        

        //交换两个值 两个入参匀为inout类型 固交换后,两值发生变化

        var oneInt = 3

        var anotherInt =  903

        swap(&oneInt,&anotherInt)

        println("oneInt:\(oneInt),anotherInt:\(anotherInt)")//oneInt:903,anotherInt:3

posted @ 2014-10-10 15:14  颜妍  阅读(382)  评论(0编辑  收藏  举报