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的過濾掉