【问题标题】:iOS query multidimensional array and return index pathiOS查询多维数组并返回索引路径
【发布时间】:2011-05-03 13:00:53
【问题描述】:

我有一个包含其他 NSMutableArrays 的 NSMutableArray(我将它们称为子数组)。这些子数组每个都有一个唯一的变量,称为 id。我想返回包含某个 id 的子数组的索引路径。例如,如果其中一个子数组的 id 为 25,如何找出该子数组的索引路径(不是子数组中 id 的索引路径)?

提前谢谢你。如果您有任何问题,请告诉我。

干杯,埃文。

【问题讨论】:

    标签: iphone ios nsmutablearray


    【解决方案1】:

    您基本上必须手动搜索所有子数组。例如:

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

    【讨论】:

    • 谢谢! indexOfObjectPassingTest 效果很好!我修改了我的代码:NSUInteger idx = [pendingFriends indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) { Friends *friends = (Friends *)[pendingFriends objectAtIndex:idx]; NSNumber *myFloat1; myFloat1 = [friends userID]; *stop = [myFloat1 isEqualToNumber:userIdentity]; return (*stop); }];
    【解决方案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 的数组的相应索引。

    .

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2014-11-11
      • 2016-03-24
      • 2013-12-04
      • 2019-06-26
      • 2014-06-27
      相关资源
      最近更新 更多