import UIKitvar str = "Hello,playground"//函数和闭包//func 声明一个函数func greet (name : String,day : String) ->String{ return "hello \(name),today is \(day)"}greet("Bob","FrIDay")func getGasPrices()->(Double,double,Double){ return (3.59,3.69,3.79)}getGasPrices()func sumof(numbers: Int...)->Int{ var sum = 0 for number in numbers{ sum += number } return sum}sumof()sumof(43,45,100)func returnFifteen() ->Int{ var y = 10 func add(){ y += 5 } add() return y}returnFifteen()func makeIncrementer() ->(Int ->Int){ func addOne(numbew:Int)->Int{ return 1 + numbew } return addOne}var increment = makeIncrementer()increment(7)func hasAnyMatches(List:Int[],condition:Int ->Bool)->Bool{ for item in List { if condition(item) { return true } } return false}func lessthanTen(number :Int)->Bool{ return number < 10}var numbers = [20,19,7,12 ]hasAnyMatches(numbers,lessthanTen)//闭包numbers.map({ (number:Int) ->Int in let result = 3 * number return result})numbers.map({number in 3 * number})sort ([1,5,3,12,21]) { > }总结
以上是内存溢出为你收集整理的Swift学习函数和闭包全部内容,希望文章能够帮你解决Swift学习函数和闭包所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)