Iphone NSDictionary在插入数据时不返回按其排列的键?

Iphone NSDictionary在插入数据时不返回按其排列的键?,iphone,nsdictionary,Iphone,Nsdictionary,我已经设置了一个NSDictionary,并且我希望它的键按其排列。但是当我打印字典日志时,它的allkeys和allkeys排序后,它会给我不同的输出。任何方法我都可以按我排列的方式获得NSDictionary键。我的代码如下:- titleDics=[[NSMutableDictionary alloc] init]; [titleDics setObject:@"Exchange" forKey:@"exchange"]; [titleDics setObject:@"Order No"

我已经设置了一个NSDictionary,并且我希望它的键按其排列。但是当我打印字典日志时,它的allkeys和allkeys排序后,它会给我不同的输出。任何方法我都可以按我排列的方式获得NSDictionary键。我的代码如下:-

titleDics=[[NSMutableDictionary alloc] init];
[titleDics setObject:@"Exchange" forKey:@"exchange"];
[titleDics setObject:@"Order No" forKey:@"nestordernumber"];
[titleDics setObject:@"Transaction Type" forKey:@"transactiontype"];
[titleDics setObject:@"Symbol Name" forKey:@"symbolname"];
[titleDics setObject:@"Price to fill" forKey:@"pricetofill"];
[titleDics setObject:@"Exchange Order ID" forKey:@"exchangeorderid"];
[titleDics setObject:@"Status" forKey:@"status"];
[titleDics setObject:@"Price Type" forKey:@"pricetype"];
[titleDics setObject:@"Duration" forKey:@"duration"];
[titleDics setObject:@"Product Code" forKey:@"productcode"];
[titleDics setObject:@"Script Name" forKey:@"scripname"];

NSLog(@"Title Dictioanry is %@",titleDics);

NSLog(@"Title dics keys are %@",[titleDics allKeys]);

NSLog(@"Title dics sorted keys are %@",[[titleDics allKeys] sortedArrayUsingSelector:@selector(compare:)]);
输出为:-----

我要按我给NSDictionary设置的顺序来设置键,即

exchange,
nestordernumber,
transactiontype,
symbolname,
pricetofill,
exchangeorderid,
status,
pricetype,
duration,
productcode,
scripname,
NSMutableDictionary
总是以无序方式返回键

如果你真的想维持秩序,那么你需要做一些技巧。像

1)在您的
键之前添加
01、02、03
序列号,以便按照您想要的顺序对它们进行排序


2)修剪字符串的前两个字符并使用它。

此链接说明如何将所需字典的键存储在所需的单独数组中,然后相应地使用它们

exchange,
nestordernumber,
transactiontype,
symbolname,
pricetofill,
exchangeorderid,
status,
pricetype,
duration,
productcode,
scripname,