IOS开发 OC Block 和swift 闭包对比学习
2019-01-16 本文已影响0人
叶叶_0217
OC的 block的写法:
https://www.jianshu.com/p/1457a4894ec7
返回值类型 (^block的名称) (block的参数)=^(参数列表){
//执行的代码
};
//例子
int (^sumOfNumbers)(int a , int b)=^(int a,int b){
/*下面为代码块*/
return a + b;
};
调用 NSLog(@"块返回:%d",myBlockC(1,5));
Swift的 闭包 的写法:
返回值类型:
{
(形参) -> (返回值类型) in
// 执行代码
}
let hero = {
(firstName:String,lastName:String,age:Int)->(String,Int) in
/*下面为代码块*/
let info:String = "名称:" + firstName + " 字: " + lastName + "\n" + "终年:" + String(age);
return (info,age);
}
调用: let b = hero("诸葛亮","孔明",56);