开发者

In what order will for(NSString * ID in someNSDictionary) run?

for(NSString * ID in someNSMutableDictionary) 
{
//do something
}

Now, will ID show up in the order they are adde开发者_Go百科d to someNSMutableDictionary?


No, the order of the keys in someNSMutableDictionary is undefined. Dictionaries are about key-value pairs, not about order.

If you need to iterate over dictionary with some order - order the keys and then iterate over the array of ordered keys and fetch each value individually.


No the IDs do not show up in the orders they were added or in any sorted order unless you force a sort on the keys of an NSDictionary object.

Random ordering of keys is a technique to minimize the time taken to access a value by key and is generally used in most dictionary or hashtable implementations.

If you want the keys to be in sorted order you can use the following code (assuming simple ASCII sort for the keys):

for (NSString* key in [[someDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)])
{
    // do stuff
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜