双层循环遍历

如下一个数组格式,里面存放着很多字典,现在有一个需求是要求取字典里的pageName对应的值,如果值有一样的,则pageRouter对应的字段为空;反之则取对应的值;1,2, 3,返回的pageRouter为空,4和5 返回的值依次为/transfer-success,/digital-receipt。

[
            {
                "flowNode": "1",
                "pageName": "info-fill",
                "pageRouter": "/info-fill"
            },
            {
                "flowNode": "2",
                "pageName": "info-fill",
                "pageRouter": "/info-fill"
            },
            {
                "flowNode": "3",
                "pageName": "info-fill",
                "pageRouter": "/info-fill"
            },
            {
                "flowNode": "4",
                "pageName": "transfer-success",
                "pageRouter": "/transfer-success"
            },
            {
                "flowNode": "5",
                "pageName": "digital-receipt",
                "pageRouter": "/digital-receipt"
            }
        ]

首先要 for循环 遍历数组,在 for循环 里面再嵌套一层循环,用字典去遍历数组,取字典的pageName字段,跟传过来的字典做匹配。
具体代码示例

// arrayFlow为数组,nextDict为传过来的字典
- (NSString *)parse:(NSMutableArray *)arrayFlow andDict:(NSDictionary *)nextDict {
    
    // 取下一个流程的pageRouter,pageName
    NSString *pageNextRouter = nextDict[@"pageRouter"];
    NSString *pageNextName = nextDict[@"pageName"];

    // 遍历数组
    for (int i = 0; i < arrayFlow.count; i++) {
       
        // 遍历数组,取数组里对应的字典,看看字典里面有没有和 nextDict 的 pageName相同的值
        for (NSDictionary *dict in arrayFlow) {
            
            NSString *tempPageName = dict[@"pageName"];
            
            if ([pageNextName isEqualToString:tempPageName]) {
                
                pageNextRouter = @"";
                return pageNextRouter;
            } else{
                
            }
        }
    }
    return pageNextRouter;
}

你可能感兴趣的:(双层循环遍历)