Block使用总结

来源:互联网 发布:关于所有淘宝软件 编辑:程序博客网 时间:2024/05/16 08:43

Block使用总结

  • Block局部变量

    returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};如:int (^sum)(int,int) = ^int(int num1,int num2){    return num1+num2;};
  • Block作为属性

    @property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);       如:@property (nonatomic,copy) int (^sum)(int,int);
  • Block作为方法参数

    - (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;如:-(void)cal:(int (^)(int,int))sum{}   
  • Block作为参数让方法调用

    [someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
  • Block typedef

    typedef returnType (^TypeName)(parameterTypes);TypeName blockName = ^returnType(parameters) {...};

    参考文档

0 0
原创粉丝点击