我有一個NSMutableArray的具有下列對象:NSMutableArray的快速搜索????/
@interface MyViewCell : UITableViewCell {
NSUInteger id;
.....
}
在一些方法,我需要與預定義的ID的小區快速搜索。如何最好的做到這一點?
我有一個NSMutableArray的具有下列對象:NSMutableArray的快速搜索????/
@interface MyViewCell : UITableViewCell {
NSUInteger id;
.....
}
在一些方法,我需要與預定義的ID的小區快速搜索。如何最好的做到這一點?
可能最簡單的方法是使用- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
。請參閱文檔here。
int index = [myArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
MyViewCell *cell = (MyViewCell *)obj;
BOOL result = (cell.id == someValue);
stop = &result;
return result;
}];
當我嘗試代碼時,遇到了一個小問題。下一個調整代碼適合我(* stop):
int index = [myArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
MyViewCell *cell = (MyViewCell *)obj;
BOOL result = (cell.id == someValue);
*stop = result;
return result;
}];
是的,這是一個小錯誤:) –
謝謝。我怎樣才能找到MyViewCell?據我所知它只是搜索預定義的ID。 –
'[myArray objectAtIndex:index]' – highlycaffeinated