上QQ阅读APP看书,第一时间看更新
How to do it...
- Go to Xcode and create a new playground file called Closures to test code about closures in it.
- To see closures in action, copy and paste this piece of code in the playground file (the output of each statement is printed on the right):
var names = ["David", "Jones", "Suzan", "Naomi", "Adam"] names.sort() // ["Adam", "David", "Jones", "Naomi", "Suzan"] names.sort{ (str1: String, str2: String) ->Bool in return str1 > str2 } // ["Suzan", "Naomi", "Jones", "David", "Adam"]