Keep NSArray original order in NSCountedSet

497 views Asked by At

I have an NSArray and I need to get data from two keys and put together in a NSMutableDictionary. One key has stringvalues and the other NSNumbervalues. When I try to create NSCountedSetwithout adding the keys I want to use to separate arrays, it doesn't work, because the objects are not identical, basically, I need to check if objectId is identical, don't matter if the other keys are different.

Here is the initial code:

for (PFObject *objeto in objects) {
        PFObject *exercicio = objeto[@"exercicio"];
        NSString *string = exercicio.objectId;
        NSNumber *nota = objeto[@"nota"];
        [exercicios addObject:string];
        [notas addObject:nota];

So I create two NSMutableArraysand store the values I need. When I logthe arrays after this, they are perfectly ordered, meaning the NSStringis in the same indexof the NSNumberit belongs to in the other array. So far, so good.

Now, when I create the NSCountedSetwith the strings, it changes the order.

NSCountedSet *countedExercicios = [[NSCountedSet alloc] initWithArray:exercicios];.

My goal is to sum the NSNumbers pertaining to an specific object, therefore, when the order changes, I lose the connection between the two arrays.

I'm not sure what I could do to solve this problem, or even if there's a different approach to achieve the result I need.

1

There are 1 answers

2
Greg On BEST ANSWER

You can create NSDictionary and add it to array. You will have just one array and you won't lose the connection, you can use objectId as a key and NSNumber as a value:

for (PFObject *objeto in objects) {
        PFObject *exercicio = objeto[@"exercicio"];
        NSString *string = exercicio.objectId;
        NSNumber *nota = objeto[@"nota"];
        NSDictionary *dict = @{string: nota};
        [newArray addObject: dict];
}

When you need get all key (objectId) you can use NSPredictate.

Hope this help