tags:

views:

11

answers:

1

How to get unique values based on dictionary key, which is an element of an array in objective C ?

for example:

i am having an array of an Items

aryItem

  aryItem[0] = Dictionary{

               ItemCategory
               ItemName
               ItemPrice

               }

  aryItem[1] = Dictionary{

               ItemCategory
               ItemName
               ItemPrice

               }

   ...........
   ...........
  aryItem[n] = Dictionary{

               ItemCategory
               ItemName
               ItemPrice

               }

Now i want to get only unique ItemCategory , not duplicates. If i can write [[aryItem objectatIndex:i] valueForKey:ItemCategory] i get all categories, same category also included in this. I need only unique categories. I have an option searching whole array and then get unique Itemcategory objects, but i am looking for any short way to accomplish the same thing.

Thanks.

+1  A: 

I think NSSet class is most suitable to collect unique values. You can create it using something like:

NSSet* categories = [NSSet setWithArray: [aryItem valueForKey: @"ItemCategory"]];
Vladimir
I have tried this, but its not working. No objects in NSSet Categories. Any other method ??
Matrix
it should work. Check that valueForKey parameter is exactly the same with the key used in dictionary. Also try to check if aryItem contains items you expect
Vladimir