开发者

iOS query multidimensional array and return index path

开发者 https://www.devze.com 2023-03-02 17:52 出处:网络
I have an NSMutableArray which holds other NSMutableArrays (I\'ll call them sub-arrays). These sub-arrays each hold开发者_StackOverflow中文版 a unique variable called id. I want to return the index pa

I have an NSMutableArray which holds other NSMutableArrays (I'll call them sub-arrays). These sub-arrays each hold开发者_StackOverflow中文版 a unique variable called id. I want to return the index path of the sub array which holds a certain id. For example, if one of the sub-arrays had an id of 25, how could I find out the index path of that sub-array (not the index path of the id in the sub-array)?

Thank you in advance. Let me know if you have any questions.

Cheers, Evan.


You will basically have to search manually through all subarrays. For example:

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];


From the NSArray class reference i have found the below method.

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

So if you have the array which hold your index id then pass it with your main array to this method and you will get the corresponding index of the array which has your id.

.

0

精彩评论

暂无评论...
验证码 换一张
取 消