How to add a value to an NSOrderedSet

2.7k Views Asked by At

I'm using a NSOrderedSet to store the data in a Json server and do it that way because data arrives duplicated and I have to filter them to keep only one value. Now I need to add a value at the beginning of NSOrderedSet. I know how to add value to an NSArray, but not how to pass data from NSOrderedSet to the NSArray.

This is an example of what I get from the server:

{(
    AMAZONAS,
    AMAZONAS,
    ANTIOQUIA,
    ANTIOQUIA,
    ARAUCA,
    "AREA EN LITIGIO",
    "AREA EN LITIGIO",
    ATLANTICO,
)}

with NSOrderedSet sorts the data to avoid repeated as follows:

    deptoArray=[NSOrderedSet orderedSetWithArray:[_data valueForKey:@"Nombre"]];

{(
    AMAZONAS,
    ANTIOQUIA,
    ARAUCA,
    "AREA EN LITIGIO",
    ATLANTICO,
)}

but now I need to add a data at the beginning so that it is as follows:

{(
    Seleccione,
    AMAZONAS,
    ANTIOQUIA,
    ARAUCA,
    "AREA EN LITIGIO",
    ATLANTICO,
)}
1

There are 1 best solutions below

0
On BEST ANSWER

The standard foundation collection classes are all immutable. So you need to create a mutable version of the collection. Either create it as a mutable ordered set

NSMutableOrderedSet *deptoArray = nil;
// ...
deptoArray= [NSMutableOrderedSet 
                 orderedSetWithArray:[_data valueForKey:@"Nombre"]];
[deptoArray insertObject:@"Seleccione" atIndex:0];

or create a mutable copy of the ordered set (skip autorelease if you use ARC)

NSMutableOrderedSet *mutableDeptoArray = 
            [[deptoArray mutableCopy] autorelease];
[mutableDeptoArray insertObject:@"Seleccione" atIndex:0];