Frage

I have a NSArray that looks like this:

{"result":
    [
        {
            "epoch":"1371333600"
        },
        {
            "epoch":"1371420000"
        },
        {
            "epoch":"1371333600"
        }
    ]
}

I want to sort the NSArray and make a new one so i can use it easier with the tableview methods to count the sections and rows.

All the dates that are the same need to be in one section.

The array should look like the example below but i don’t know how to get there. I have tried NSPredicate and used a loop but it won’t work.

What i want:

{"result":
    [
        {"data":
            [
                {
                    "epoch":"1371333600"
                },
                {
                    "epoch":"1371333600"
                }
            ]
        },
        {"data":
            [
                {
                    "epoch":"1371420000"
                }
            ]
        }
    ]
}

My NSPredicate looks like this, but does not give me the result.

_finalArray = [[NSMutableArray alloc] init];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"epoch IN %@", [_resultArray valueForKey:@"epoch"]];
_predicateDate = [NSMutableArray arrayWithArray:[dataSortArray filteredArrayUsingPredicate:predicate]];

if ([_predicateDate count] != 0) 
    {
        NSDictionary *itemsArrayDict = [NSDictionary dictionaryWithObject:_predicateDate forKey:@"data"];
        [_finalArray addObject:itemsArrayDict];
    }
War es hilfreich?

Lösung

NSOrderedSet is awesome for this occasion as it allows you to get the unique strings.

NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"2222222" forKey:@"epoch"];
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"2222222" forKey:@"epoch"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObject:@"1111111" forKey:@"epoch"];

NSArray *dictArray = @[dict1, dict2, dict3];
NSMutableArray *finalArray = [[NSMutableArray alloc]init];
NSArray *epoches = [dictArray valueForKey:@"epoch"];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:epoches];

for (NSString *string in orderedSet) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"epoch == %@", string];
    NSArray *resultsArray = [dictArray filteredArrayUsingPredicate:predicate];
    [finalArray addObject:resultsArray];
}

Andere Tipps

Hi you can use the NSPredicate to filter an array like:

//NSPredicate to filter an array
NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"hello" forKey:@"Test"]];    
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(Test == %@)", @"hello"]];

Thanks

It appears that you are using dictionaries along with arrays. Here's what I've achieved:

(result 
    (data
            {
        epoch = 1371333600;
    },
            {
        epoch = 1371333600;
    }
),
    (data
            {
        epoch = 1371420000;
    }
)

)

I'm not using predicates, but it looks as it's working :). Here is the code:

// Initial input
NSArray *result = @[@{@"epoch":@"1371333600"},@{@"epoch":@"1371420000"},@{@"epoch":@"1371333600"}];

// Get unique values for the input
NSSet *resultSet = [NSSet setWithArray:result];

// Here we are going to store the final result
NSMutableArray *newResult = [[NSMutableArray alloc] init];

// Loop over the unique items
for (NSDictionary *uniqueItem in resultSet) {

    // Here we are going to store the grouped data
    NSMutableArray *dataResult = [[NSMutableArray alloc] init];

    // Loop over the initial input
    for (NSDictionary *resultItem in result) {

        // Search for all the items that are equal to the uniqe
        // I would rather include a count instead of repeating values :)
        if([uniqueItem isEqual:resultItem]) {
            [dataResult addObject:resultItem];
        }
    }

    [newResult addObject:dataResult];
}

NSLog(@"%@", newResult);

Cheers!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top