0

我已经设置了一个 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:)]);

输出是:-----

2013-05-30 12:51:43.924 MobileTrading[1676:11303] Title Dictioanry is {
duration = Duration;
exchange = Exchange;
exchangeorderid = "Exchange Order ID";
nestordernumber = "Order No";
pricetofill = "Price to fill";
pricetype = "Price Type";
productcode = "Product Code";
scripname = "Script Name";
status = Status;
symbolname = "Symbol Name";
transactiontype = "Transaction Type";
}
2013-05-30 12:51:43.924 MobileTrading[1676:11303] Title dics keys are (
    nestordernumber,
    productcode,
    symbolname,
    pricetype,
    exchange,
    exchangeorderid,
    pricetofill,
    scripname,
    status,
    duration,
    transactiontype
)
2013-05-30 12:51:43.924 MobileTrading[1676:11303] Title dics sorted keys are (
    duration,
    exchange,
    exchangeorderid,
    nestordernumber,
    pricetofill,
    pricetype,
    productcode,
    scripname,
    status,
    symbolname,
    transactiontype
)

我希望这些键按照我设置为 NSDictionary 的顺序,即;

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

2 回答 2

0

NSMutableDictionary总是以无序的方式返回键。

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

1)在您之前添加01, 02 ,03 序列号Keys,按您想要放置的顺序对它们进行排序。

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

于 2013-05-30T07:31:53.300 回答
0

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

NSDictionary 中的键和值是否有序?

于 2013-05-30T08:18:35.263 回答