NSDictionary 的有序性 (by the key in some rule)
NSDictionary 的有序性: (by the key in some rule)
NSDictionary*myDictionary =[NSDictionary dictionaryWithObjectsAndKeys:@"o1",@"k1",@"o6",@"k4",@"o2",@"k2",@"o3",@"k3",@"o5",@"k5", nil];NSInteger count =[myDictionary count];
如下代码1:
NSLog(@"%@ ", [myDictionary allKeys]);
NSLog(@"%@ ", [myDictionary allValues]);
每次运行都是这样的结果
2014-04-10 10:55:58.548[3105:70b] (
k4,
k3,
k2,
k5,
k1
)
2014-04-10 10:55:58.548[3105:70b] (
o6,
o3,
o2,
o5,
o1
)
修改k4 --> k6
每次运行结果为
2014-04-10 10:53:38.318[3083:70b] (
k3,
k6,
k2,
k5,
k1
)
2014-04-10 10:53:38.318[3083:70b] (
o3,
o6,
o2,
o5,
o1
)
如下代码2:
id __unsafe_unretained objects[count];
id __unsafe_unretained keys[count];
[myDictionary getObjects:objects andKeys:keys];
for(int i =0; i < count; i++){
id obj = objects[i];
id key = keys[i];NSLog(@"%@ -> %@", obj, key);
}
每次运行都是这样的结果
o6 -> k4
o3 -> k3
o2 -> k2
o5 -> k5
o1 -> k1
修改k4 --> k6
每次运行结果为
o3 -> k3
o6 -> k6
o2 -> k2
o5 -> k5
o1 -> k1
由此可知
NSDictionary 其实是有序的: (by the key in some rule) 根据key进行排序