我有一个NSMutableArray,其中包含其他NSMutableArrays(我将称它们为子数组)。这些子数组每个都拥有一个称为id的唯一变量。我想返回包含某个id的子数组的索引路径。例如,如果其中一个子数组的ID为25,我如何才能找出该子数组的索引路径(而不是子数组中的id的索引路径)?iOS查询多维数组并返回索引路径
预先感谢您。如果您有任何问题,请告诉我。
干杯,埃文。
我有一个NSMutableArray,其中包含其他NSMutableArrays(我将称它们为子数组)。这些子数组每个都拥有一个称为id的唯一变量。我想返回包含某个id的子数组的索引路径。例如,如果其中一个子数组的ID为25,我如何才能找出该子数组的索引路径(而不是子数组中的id的索引路径)?iOS查询多维数组并返回索引路径
预先感谢您。如果您有任何问题,请告诉我。
干杯,埃文。
您基本上必须通过所有子阵列手动搜索。例如:
NSUInteger outerIndex = [outerArray indexOfObjectPassingTest:^(id subarray, NSUInteger outerIdx, BOOL *stopOuter) {
NSUInteger innerIndex = [subarray indexOfObjectPassingTest:^(id obj, NSUInteger innerIdx, BOOL *stopInner) {
return [obj isEqual:searchTerm];
}];
return innerIndex != NSNotFound;
}];
NSUInteger indexes[] = {outerIndex, innerIndex};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2];
从NSArray class reference我发现了下面的方法。
indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object.
- (NSUInteger)indexOfObject:(id)anObject
Parameters
anObject
An object.
Return Value
The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.
Discussion
Objects are considered equal if isEqual: returns YES
所以,如果你有握住你的索引ID数组,然后用你的主数组这种方法通过它,你将获得具有您的ID阵列的相应指标。
。
谢谢! indexOfObjectPassingTest非常棒!我修改代码作为这样: 'NSUInteger IDX = [pendingFriends indexOfObjectPassingTest:^(ID OBJ,NSUInteger IDX,BOOL *停止) \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t友*朋友=(朋友*)[pendingFriends objectAtIndex:idx]; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t的NSNumber * myFloat1; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t myFloat1 = [朋友用户ID]; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t *停止= [myFloat1 isEqualToNumber:的UserIdentity]; \t \t \t \t \t \t \t \t \t回报(*停止); \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t}];' – 2011-05-04 03:56:06