博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
block
阅读量:7023 次
发布时间:2019-06-28

本文共 2087 字,大约阅读时间需要 6 分钟。

This is a block

^{    NSLog(@"This is from a block");}

a block with arguments

^(double dividend, double divisor){    double quotient = dividend/divisor;    return quotient;}

although blocks look like functions, they can be stored in variables. Like other variables, block variables are declared and then assigned values.

//declare the block variablevoid (^devowelizer) (id, NSUInteger, BOOL *);

void : return type of block

^    : indication that this is a block

devowelizer: name of block variable

comma-delimited arguments

//assign a block to the variabledevowelizer =  ^(id string, NSUInteger i, BOOL *stop){    NSMutableString *newString=[NSMutableString stringWithString:string];    for (NSString *s in vowels){        NSRange fullRange = NSMakeRange(0,[newString length]);        [newString replaceOccurrencesOfString:s                                   withString:@""                                      options:NSCaseInsensitiveSearch                                        range:fullRange];    }    [newStrings addObject:newString];};//end of block assignment

Passing in a block

Because devowelizer is a variable, you can pass it as an argument. NSArray has a method called enumerateObjectsUsingBlock: This method expects a block as its sole argument, it will execute that block once for each object in the array.

[oldStrings enumerateObjectsUsingBlock:devowelizer];

 

Add a check at the beginning of the block

devowelizer = ^(id string, NSUInteger i, BOOL *stop){    NSRange yRange = [string rangeOfString:@"y"                                       options:NSCaseInsensitiveSearch];    //did i find a y?    if(yRange.location != NSNotFound){         *stop = YES;         return;    }    ...};

typedef

Block syntax can be confusing, but you can make it friendlier using the typedef keyword.

Remember that tyepdef belongs at the top of the file or in a header, outside of any method implementations.

#import 
typedef void (^ArrayEnumerateBlock)(id,NSUInteger,BOOL *); ... ArrayEnumerateBlock devowelizer;

 

 

 

 

转载于:https://www.cnblogs.com/grep/archive/2012/06/12/2546010.html

你可能感兴趣的文章
矩阵覆盖,基本DP题目
查看>>
定义一个不能被继承的类
查看>>
xgboost参数调优的几个地方
查看>>
python3编写网络爬虫13-Ajax数据爬取
查看>>
JVM监控启动参数
查看>>
npm 是干什么的
查看>>
视达配色教程1 色彩是什么
查看>>
枚举2--熄灯问题
查看>>
overload和override二者之间的区别
查看>>
Spring MVC工作原理(好用版)
查看>>
html5--1.18 div元素与布局
查看>>
HTML.7表单
查看>>
企业架构研究总结(7)——联邦企业架构之FEAF的出现和构成(下)
查看>>
5.3 删除二叉搜索树的最大元素和最小元素
查看>>
stund客户端使用结果说明
查看>>
东西学了容易忘?学会跟踪你的知识
查看>>
6.13
查看>>
鱼C扫描器
查看>>
hdu 5720
查看>>
选择区间不相交问题
查看>>