NSMutableDictionary, different print from console and code

前提是你 提交于 2019-12-25 04:21:01

问题


I have some questione about NSMutableDictionary. I read that they are only a wrapper around an hashmap, so their use for ordered list is discouraged.

But I have to use it, so the problems began...

I save the result of a Json (after a call to an URL) in a NSMutableDictionary and then I have two different "print":

  • if I try to print the dictionary from console with command "po myDictionary" I have an order (the order of the original json)
  • if I create a simple "for statement" and print every elements in myDictionary, I have a different order.

How is it possible? Shouldn't they have the same order?

Thank to all!

EDIT: this is a little example of the code:

NSMutableDictionary *myDictionary = [--{RECEIVED FROM ANOTHER CALL}--];
NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithCapacity:[myDictionary count]];

for(NSString *key in myDictionary) {
    NSLog(@"%@", key);

    NSObject *object = [[NSObject alloc] init]

    /* some operation in the code with object */

    [tmp setObject:object forKey:key];
}

After the "for statement" this is what happened:

  • if I see all the logs I receive something (for example if myDictionary has the name of the day I have something like "Saturday, Monday, Friday.....")
  • if I insert a breakpoint here and try to print something from console with the command "po myDictionary" I have another order, ("sunday, tuesday, monday, ...")

The question is: why?

EDIT 2: I know that there exists a system to have an "OrderedDictionary", but it isn't the question: I want to understand why I have two different print, one from the "for statement" and the other from "po command".


回答1:


The po <object> command in gdb prints the output of [<object> description]. [NSDictionary description] sorts its output by key. This is documented behavior.

If your JSON is sorted by key (which it sounds like it is), then you can just sort by key again to ensure a reliable order.

Note that if your JSON is using an object (a collection of key/value pairs) to represent ordered data, it is in violation of the JSON spec.

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

The correct encoding is an array of single-key objects such as:

[ { "firstKey": "value" }, { "secondKey": "value" } ]

But if you can use sorted keys to define order, that's appropriate.




回答2:


NS(Mutable)Dictionaries aren't ordered, period. (It's not just discouraged.) There's no guarantee that even if you got the "original order" that po provides, it'd stay that way from app launch to app launch, or from OS version to OS version, or from hardware to hardware. Don't do that.

Maybe you want to use something like http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html?




回答3:


Hashmap do not have key or value order,and not promise any order. If you need same order, you can use array of key-value pair, or use http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html .




回答4:


You might prefer an ordered dictionary: http://cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html

Okay, you've now modified your original question. You're now asking:

I know that there exists a system to have an "OrderedDictionary", but it isn't the question: I want to understand why I have two different print, one from the "for statement" and the other from "po command".

That's because they are two completely different ways of accessing the data. The for statement walks the keys one by one in some order. The po command calls the dictionary's description method, which clearly does not consist of a for statement enumerating the keys like yours. I don't understand why you find this puzzling.



来源:https://stackoverflow.com/questions/8360100/nsmutabledictionary-different-print-from-console-and-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!