Question

I saw many examples on SO but I'm not sure if it applies to this situation. Everywhere it says NSMutableDictionries are not guaranteed an order...but I'm getting my data from the server...so my NSMuteDic looks like this:

{
joe =     (
            {
        fromName = joe;
        id = 25;
        theMessage = "this is going to be a really big message...";
        timeAdded = "2014-04-07 21:08:12";
        toName = "me";
    },
            {
        fromName = joe;
        id = 10;
        theMessage = "why???";
        timeAdded = "2014-04-05 20:10:04";
        toName = "me";
    }
);
bob =     (
            {
        fromName = "me";
        id = 24;
        theMessage = "blah blah";
        timeAdded = "2014-04-06 21:15:06";
        toName = bob;
    },
            {
        fromName = bob;
        id = 22;
        theMessage = message;
        timeAdded = "2014-04-06 20:11:57";
        toName = "me";
    }
);
//more entries here
}

What I want to do is change the order...put bob first and joe second. Is this really impossible to do? I saw many very complex solutions...there's no easy way to do this with just a for loop?

cellForRowAtIndexPath: NSMutableArray *temp = [myDict objectForKey:keys[indexPath.row]]; cell.Message.text = [[reverse lastObject] valueForKey:@"theMessage"]; cell.dateTime.text = [[reverse lastObject] valueForKey:@"timeAdded"]; return cell;

This is how I'm using it...and when a row is selected I pass the array to the next view controller. The reason why I want to reorder is if a new message will be inserted in the pushed view controller, I want that dictionary to be first in the list so the root view controller can be reordered.

NSArray *keys = [myDict allKeys];
[myDict removeObjectForKey:to];
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:[myDict.count];
temp = myDict;
[myDict removeAllObjects];
[myDict setObject:currentDict forKey:to];
for (int i=0; i<temp.count; i++) {
    [myDict setObject:[temp valueForKey:keys[i]] forKey:keys[i]];
}

That's not working because it looks like since myDict is a NSObject, temp gets changed every time myDict changes...from the looks of it the logic should work but it isn't...

Était-ce utile?

La solution

NSMutableDictionary is not ordered. There is nothing you can do to guarantee the order of keys because NSDictionary makes no attempt to preserve any particular ordering. To go over a dictionary in a particular order, you have to make an array of the keys that is in the order you want, then iterate that and fetch the corresponding values.

Autres conseils

// Get the keys
NSArray *keys = [myDict allKeys];

// Sort the keys
NSArray *sortedArray = [NSArray arrayWithArray:[keys sortedArrayUsingComparator:^(NSString* a, NSString* b) {
    return [a compare:b];
}]];

// Iterate the dictionary
for (NSUInteger n = 0 ; < [sortedArray count]; n++) {
    id value = [myDict objectForKey:[sortedArray objectAtIndex:n]];

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top