例子1:
使用sort函数
let names = ["A-a","C","B-b","D"]let reversed = names.sort(backwards)func backwards(firstStr: String,secondStr: String) -> Bool { return firstStr < secondStr; }
例子2:
自定义对象
class Model { var name = String(); var age = String(); init(name: String,age: String){ self.name = name; self.age = age; }; }let models = [Model.init(name: "A-a",age: "12"),Model.init(name: "D",age: "11"),Model.init(name: "B-b",age: "21"),Model.init(name: "C-c",age: "19")] let nameResult = models.sort { (ml1,ml2) -> Bool in return ml1.name < ml2.name; } for ml in nameResult { print(ml.name,ml.age); } print("------------------") let ageResult = models.sort {A-a 12B-b 21C-c 19D 11------------------11 D12 A-a19 C-c21 B-b.age < .age } for ml in ageResult { print(ml.age,ml.name); }
输出:
参考:http://www.jianshu.com/p/ad71c94e7bc6
总结以上是内存溢出为你收集整理的swift 闭包全部内容,希望文章能够帮你解决swift 闭包所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)