NSMutableArray快速搜索???? /

Rom*_*anS 0 objective-c nsmutablearray

我有一个带有以下对象的NSMutableArray:

  @interface MyViewCell : UITableViewCell {

    NSUInteger id;
    .....
  }
Run Code Online (Sandbox Code Playgroud)

在某些方法中,我需要快速搜索具有预定义id的单元格.怎么做到最好?

hig*_*ted 9

可能最简单的方法就是使用- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate.见文档这里.

int index = [myArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
    MyViewCell *cell = (MyViewCell *)obj;
    BOOL result = (cell.id == someValue);
    stop = &result;
    return result;
}];
Run Code Online (Sandbox Code Playgroud)