Objective c 基于数字重新组织NSDictionary键
我有一本字典,看起来像:Objective c 基于数字重新组织NSDictionary键,objective-c,ios,cocoa-touch,nsdictionary,Objective C,Ios,Cocoa Touch,Nsdictionary,我有一本字典,看起来像: 0 = { content = Test; date = "2012-02-02 18:36:46 +0000"; }; 1 = { content = "#### da ####"; date = "2012-02-02 18:36:46 +0000"; }; 2 = { content = dfdffdfddfdffddf;
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
1 = {
content = "#### da ####";
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
但是,如果我删除第二个键(示例中的数字1),字典的结果如下:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
我如何重新组织它们,以便在需要时保持顺序(如0、1、2、3)?NSDictionary是无序的,这意味着您无法控制键的顺序,并且它们可能在iOS版本之间以随机或未记录的方式更改 您有几个选择:
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingSelector:@selector(compare:)];
//now loop over keys array instead of directly over dictionary
NSDictionary是无序的,这意味着您无法控制键的顺序,并且它们可能在iOS版本之间以随机或未记录的方式更改 您有几个选择:
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingSelector:@selector(compare:)];
//now loop over keys array instead of directly over dictionary
为什么不直接使用一个NSMutableArray的NSDictionaires?“重组”我从来没有。为什么不直接使用一个NSMutableArray的NSDictionaires?“重组”我从来没有。