如何在NSMutableDictionary中比较NSArray的元素?

时间:2015-03-17 10:15:07

标签: ios objective-c nsarray nsdictionary

我的NSMutableDictionary包含四个NSArray及其各自的键。这些NSArray的大小为2,包含2D坐标。我希望得到它们中最常见的坐标。例如,如果坐标对于三个数组是通用的,那么它将是我的第一选择。如何找到至少两个数组的任何坐标?

3 个答案:

答案 0 :(得分:2)

基本上你想找到具有最大出现次数的坐标对。 因此,您可以创建所有坐标的数组并找到其模式。

    NSMutableArray *allCoordinates = [NSMutableArray new];
    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if ([key isEqualToString:@"arrayKey1"] || [key isEqualToString:@"arrayKey2"]) {
        NSArray *coordinates = (NSArray *)obj;
        [coordinates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [allCoordinates addObject:coordinates];
        }];
    }
    }];

现在,你需要编写一个自定义方法来查找坐标数组的模式(频率的附加条件是> = 3)。

答案 1 :(得分:0)

这是一个有效的例子。我假设你的字典看起来像coordinatesDict

NSDictionary *coordinatesDict = @{@"first": @[@11.58, @40.20], @"second": @[@12.12, @100.12], @"third": @[@11.58, @40.20], @"fourth": @[@13.2, @14.5]};

NSCountedSet *coordinates = [NSCountedSet setWithArray:[coordinatesDict allValues]];

for (NSArray *coordinateArray in coordinates) {

    NSUInteger coordinateCount = [coordinates countForObject:coordinateArray];
    NSLog(@"Array %@ is included %lu times", coordinateArray, (unsigned long)coordinateCount);

    if (coordinateCount >= 2) {
        // You found your best coordinate
    }
}

答案 2 :(得分:0)

以下是一些应该有效的代码,使用NSCountedSet 简而言之: 我使用NSCountedSet作为NSSet,同时保留发生次数(重复次数)。
然后,我创建一个NSArray,根据出现的次数对值进行排序 我明确写了“count1 / count2”比较,以防你想要在出现次数相同时应用不同的值。

NSDictionary *allData = @{@"Key1: %@":@[@(0.0), @(0.1)],
                          @"Key2: %@":@[@(0.1), @(0.1)],
                          @"Key3: %@":@[@(0.2), @(0.1)],
                          @"Key4: %@":@[@(0.1), @(0.1)]};

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:[allData allValues]];

for (NSArray *array in countedSet)
    NSLog(@"For %@ counted %@ time(s)", array, @([countedSet countForObject:array]));

NSArray *sortedCountedSetArray = [[countedSet allObjects] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    NSUInteger count1 = [countedSet countForObject:obj1];
    NSUInteger count2 = [countedSet countForObject:obj2];
    if (count1 < count2)
        return NSOrderedDescending;
    if (count1 > count2)
        return NSOrderedAscending;
    else
        return NSOrderedSame; //May want to do additionaly thing (for example if coordinate is closer to actual position, etc.)
}];
NSLog(@"sortedCountedSetArray: %@", sortedCountedSetArray);

NSArray *bestOption = [sortedCountedSetArray firstObject]; //Coordinates the most "popular"
NSLog(@"BestOption: %@", bestOption);

输出结果为:

> For (
    "0.1",
    "0.1"
) counted 2 time(s)
> For (
    "0.2",
    "0.1"
) counted 1 time(s)
> For (
    0,
    "0.1"
) counted 1 time(s)
> sortedCountedSetArray: (
        (
        "0.1",
        "0.1"
    ),
        (
        "0.2",
        "0.1"
    ),
        (
        0,
        "0.1"
    )
)
> BestOption: (
    "0.1",
    "0.1"
)