您现在的位置是:首页 > 文章详情

Swift中的闭包例子

日期:2018-04-19点击:372

常见的实现,

要熟悉了解,

至于闭包逃逸,

自动闭包这些内容,

可以以后用到时再学吧。



let names = ["Chris", "Alex", "Eva", "Barry", "Daniella"] func backward(_ s1: String, _ s2: String) -> Bool { return s1 < s2 } var rNames = names.sorted(by: backward) rNames = names.sorted(by: {(s1: String, s2: String) -> Bool in return s1 > s2 }) rNames = names.sorted(by: {(s1: String, s2: String) -> Bool in return s1 > s2}) rNames = names.sorted(by: {s1, s2 in return s1 > s2}) rNames = names.sorted(by: {s1, s2 in s1 > s2}) rNames = names.sorted(by: {$0 > $1}) rNames = names.sorted(by: >) rNames = names.sorted(){$0 > $1} rNames = names.sorted{$0 > $1} print(rNames) let digitNames = [ 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine" ] let numbers = [16, 58, 510] let strings = numbers.map { (number) -> String in var number = number var output = "" repeat { output = digitNames[number % 10]! + output number /= 10 } while number > 0 return output } print(strings) func makeIncrementer(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementer() -> Int { runningTotal += amount return runningTotal } return incrementer } let incrementByTen = makeIncrementer(forIncrement: 10) print(incrementByTen()) print(incrementByTen()) print(incrementByTen()) print(incrementByTen()) 


原文链接:https://yq.aliyun.com/articles/583444
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章