Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 多维数组中的计数_Ios_Objective C - Fatal编程技术网

Ios 多维数组中的计数

Ios 多维数组中的计数,ios,objective-c,Ios,Objective C,您将如何计算此数组 NSArray *sortThisArray = @[@{@"numbers":@[@"One",@"Two"]}, @{@"numbers":@[@"Two",@"One"]}, @{@"numbers":@[@"One",@"Two",@"Three"]}, @{@"numbers":@[

您将如何计算此数组

NSArray *sortThisArray = @[@{@"numbers":@[@"One",@"Two"]},
                               @{@"numbers":@[@"Two",@"One"]},
                               @{@"numbers":@[@"One",@"Two",@"Three"]},
                               @{@"numbers":@[@"One",@"Two",@"Three"]},
                               @{@"numbers":@[@"One",@"Two",@"Three",@"Four"]},
                               ];
然后,期望的结果将是:

NSArray *sortedArray =   @[@{@"numbers":@[@"One",@"Two"],
                             @"occures":@(2)},

                             @{@"numbers":@[@"One",@"Two",@"Three"],
                             @"occures":@(2)},

                             @{@"numbers":@[@"One",@"Two",@"Three",@"Four"],
                             @"occures":@(1)},
                          ];

我试过使用
NSCountedSet
countForObject
,但结果不准确。它似乎只计算完全相同的数组。换句话说,具有
@[@“二”,“一”]
的数组将被忽略,因为它不是100%等于
@[@“一”,“二”]
,即使它们具有相同的对象和相同的计数。

可能是这样的:

NSMutableArray *mArray = [NSMutableArray arrayWithArray:sortThisArray];
NSMutableArray *result = [[NSMutableArray alloc] init];


while ([mArray count]) {
    NSDictionary *obj = [mArray firstObject];
    int count = 1;
    for (int i=1; i<[mArray count]; i++) {
        NSDictionary *sObj = [mArray objectAtIndex:i];
        if ([[sObj objectForKey:@"numbers"] count] == [[obj objectForKey:@"numbers"] count]) {
            BOOL increase = YES;
            for (int j=0; j<[[obj objectForKey:@"numbers"] count]; j++) {
                if (![[sObj objectForKey:@"numbers"] containsObject:[[obj objectForKey:@"numbers"] objectAtIndex:j]]) {
                    increase = NO;
                }
            }
            if (increase) {
                count++;
                [mArray removeObjectAtIndex:i];
            }
        }
    }
    [mArray removeObjectAtIndex:0];
    [result addObject:@{@"numbers":obj, @"occurrs":[NSNumber numberWithInteger:count]}];
}
NSMutableArray*mArray=[NSMutableArray arrayWithArray:sortThisArray];
NSMUTABLEARRY*结果=[[NSMUTABLEARRY alloc]init];
而([mArray count]){
NSDictionary*obj=[mArray firstObject];
整数计数=1;

for(int i=1;i可能是这样的:

NSMutableArray *mArray = [NSMutableArray arrayWithArray:sortThisArray];
NSMutableArray *result = [[NSMutableArray alloc] init];


while ([mArray count]) {
    NSDictionary *obj = [mArray firstObject];
    int count = 1;
    for (int i=1; i<[mArray count]; i++) {
        NSDictionary *sObj = [mArray objectAtIndex:i];
        if ([[sObj objectForKey:@"numbers"] count] == [[obj objectForKey:@"numbers"] count]) {
            BOOL increase = YES;
            for (int j=0; j<[[obj objectForKey:@"numbers"] count]; j++) {
                if (![[sObj objectForKey:@"numbers"] containsObject:[[obj objectForKey:@"numbers"] objectAtIndex:j]]) {
                    increase = NO;
                }
            }
            if (increase) {
                count++;
                [mArray removeObjectAtIndex:i];
            }
        }
    }
    [mArray removeObjectAtIndex:0];
    [result addObject:@{@"numbers":obj, @"occurrs":[NSNumber numberWithInteger:count]}];
}
NSMutableArray*mArray=[NSMutableArray arrayWithArray:sortThisArray];
NSMUTABLEARRY*结果=[[NSMUTABLEARRY alloc]init];
而([mArray count]){
NSDictionary*obj=[mArray firstObject];
整数计数=1;

对于(int i=1;i这应该可以。您必须有一种一致的方法来比较数组(可以像本例中那样排序,也可以将NSArray移动到NSSet)

NSMutableDictionary*valueCount=[NSMutableDictionary];
for(sortThisArray中的NSDictionary*值){
NSArray*numberString=[value[@“numbers”]sortedarray使用选择器:@selector(比较:)];
valueCount[NumberString]=@(valueCount[NumberString].integerValue+1);
}
NSMutableArray*sortedArray=[NSMutableArray阵列容量:valueCount.count];
用于(NSArray*输入valueCount){
[sortedArray addObject:@{@“numbers”:key,@“occurrents”:valueCount[key]};
}

这应该行得通。您必须有一种一致的方法来比较阵列(可以像本例中那样排序,也可以将NSArray移动到NSSet)

NSMutableDictionary*valueCount=[NSMutableDictionary];
for(sortThisArray中的NSDictionary*值){
NSArray*numberString=[value[@“numbers”]sortedarray使用选择器:@selector(比较:)];
valueCount[NumberString]=@(valueCount[NumberString].integerValue+1);
}
NSMutableArray*sortedArray=[NSMutableArray阵列容量:valueCount.count];
用于(NSArray*输入valueCount){
[sortedArray addObject:@{@“numbers”:key,@“occurrents”:valueCount[key]};
}

尝试过,似乎不起作用…在结果数组中仍然获取重复项将其干燥,似乎不起作用…在结果数组中仍然获取重复项