IOS中block的使用
程序员文章站
2023-01-24 22:51:46
block是ios4.0开始推出的,其特点是可以执行内联,作为参数传递到函数,block变量的定义,^ 用于运算符。
-(id)loadnibwithclassnam...
block是ios4.0开始推出的,其特点是可以执行内联,作为参数传递到函数,block变量的定义,^ 用于运算符。
-(id)loadnibwithclassname:(nsstring*)classname
{
class class = nsclassfromstring(classname);
nsarray *objects = [[nsbundle mainbundle] loadnibnamed:classname owner:nil options:nil];
int index = [objects indexofobjectpassingtest:^bool(id obj, nsuinteger idx, bool *stop) {
return [obj iskindofclass:class];
}];
assert(index != nsnotfound);
return [objects objectatindex:index];
}
上述函数根据nib创建一个class,在函数中内联了block函数,遍历objets,判断其是不是是创建好的类,将不是class的过滤掉
下一篇: 别跑分!3万块比3000块的电脑好在哪?