如题
1 | let names = ["aleige","cdameng","bhouzi"] var reversed = (names, { (firstString: String, secondString: String) -> Bool in return firstString > secondString }) reversed = (names, >) reversed = (names, { firstString, secondString in return firstString > secondString}) reversed = (names, { firstString, secondString in firstString > secondString}) reversed = (names, { $0 > $1 }) reversed = (names, >) var odds = [Int]() for i in 1...10 { if i % 2 == 1 { odds.append(i) } } print(odds) odds = Array(1...10).filter { $0 % 2 == 1 } print(odds) func plusAandB(a:Int) (b:Int) -> Int { return a+b } let plusFive = plusAandB(5) let plusTen = plusFive(b: 10) let plus11 = plusFive(b: 11) print(plus11) //var //let var array = [1,2,3,4,5] func minMax(array: [Int]) -> (min:Int, max:Int) { var currentMin = array[0] var currentMax = array[0] for value in array[1..<array.count] { if value < currentMin { currentMin = value }else if value > currentMax { currentMax = value } } return(currentMin, currentMax) } print(minMax(array)) print(minMax(array).max) print(minMax(array).min) func averageNumbers(numbers:Double...) -> Double { var total:Double = 0 for number in numbers { total += number } return total / Double(numbers.count) } //inout // _ func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } var someInt = 3 var anotherInt = 107 swapTwoInts(&someInt,b: &anotherInt) print("someInt is now \(someInt), and anotherInt is now \(anotherInt)") func chooseSayHelloFunction(lang: String) -> (String) -> String{ func sayHelloInChinese(name: String) ->String{ return "你好 "+name } func sayHelloInEnglish(name: String) -> String{ return "hello "+name } return lang == "cn" ? sayHelloInChinese: sayHelloInEnglish } let sayHelloInChinese = chooseSayHelloFunction("cn") print(sayHelloInChinese("d哆啦A梦")) func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA } var aString = "a" var bString = "b" swapTwoValues(&aString,b: &bString) print("aString is now \(aString), and bString is now \(bString)") var aInt = 55 var bInt = 99 swapTwoValues(&aInt,b: &bInt) print("aInt is now \(aInt), and bInt is now \(bInt)") |
by peixl