swift closure

前端之家收集整理的这篇文章主要介绍了swift closure前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//: Playground - noun: a place where people can play

//闭包closure,类似c语言的函数指针或ocblocks//闭包可以让代码显得非常简洁

importCocoa

////////////////////////////函数定义

funciscontain(arr:[Int],value:Int,cb:(num:Int)->Bool) ->Bool{

foriinarr{

if(cb(num:i,small:value)){

returntrue;

}

}

returnfalse;

}

////////////////////////////传统

funclessthan(value:returnvalue<less;

}


varvarr = [20,40,216)">67,216)">10,216)">44,216)">22,216)">99,216)">60];


varb =iscontain(varr,11);//调用


printlnb);



str ="Hello,playground"


////////////////////////////closure

varb1 =iscontain(varr,216)">98,

{(num:Boolinreturnnum>small;}

);//闭包调用

varb2 =

iscontain98return$0>$1;});//另一种调用方法//$0表示第一个参数,$1表示第二个参数




///////////////////////特`别`的///////////////////////////
//: Playground - noun: a place where people can play import Cocoa //字符串排序,有简到繁 let names=["zxh","xiaoming","lily","anna","daty","1jack"]; var sortedname = names.sorted({$0<$1}); sortedname; var sorted1 = names.sorted({s1,s2 in s1<s2}); sorted1; var sorted2 = names.sorted({(s1:String,s2:String)->Bool in return s1<s2}); sorted2; func compareName(s1:String,s2:String)->Bool{ return s1<s2; } var sorted3 = names.sorted(compareName);

原文链接:https://www.f2er.com/swift/326950.html

猜你在找的Swift相关文章