NSPredicate simulate an SQL "Select count (distinct columnName)" of an array

0

I have an Array full of 'n' dictionaries. These dictionaries have the same numbers of fields 'x' fields, with the same name (As a simulation of a typical database table). Field 'B' for example may contain a number or name. I want to know how many different elements there are of a value that matches the field 'B' in this Array.

To select a field with a value I do it this way,

'NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "B IN% @", @ [value]];'

But it returns all the fields whether they are different or not. How would I have to do it to return only the different ones? And then count them? That is, it would be similar to doing SQL when doing a 'SELECT COUNT (DISTINCT columnName) FROM tableName'

    
asked by Popularfan 21.03.2017 в 16:01
source

1 answer

1

Test:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(B != %@)",valor];

NSInteger count = [[ArrayDeDiccionarios filteredArrayUsingPredicate:predicate] count];

This predicate gathers all the occurrences of the Array in which the key B of the json is different from a given value. Then a count is made to the resulting array and there you have the number of different occurrences.

    
answered by 09.05.2017 в 16:57