NSDictionary 在插入数据时不返回键的排列方式?
Posted
技术标签:
【中文标题】NSDictionary 在插入数据时不返回键的排列方式?【英文标题】:NSDictionary not returns keys as it arranged while inserting data? 【发布时间】:2013-05-30 07:25:35 【问题描述】:我已经设置了一个 NSDictionary 并且我希望它的键作为它的排列。但是当我打印字典日志时,它的所有键和所有键排序它会给我不同的输出。任何方式我都可以获得 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,
【问题讨论】:
【参考方案1】:
NSMutableDictionary
总是以无序的方式返回键。
如果您真的想维持秩序,那么您需要做一些技巧。喜欢...
1) 在您的Keys
之前添加01, 02 ,03
序列号 以按照您想要的顺序对其进行排序。
2) 修剪字符串的前两个字符并使用它。
【讨论】:
【参考方案2】:此链接说明将所需字典的键存储在您想要的单独数组中,然后相应地使用它们
Are keys and values in an NSDictionary ordered?
【讨论】:
以上是关于NSDictionary 在插入数据时不返回键的排列方式?的主要内容,如果未能解决你的问题,请参考以下文章
使用特定键的值过滤 NSDictionary 的 NSArray
更新/更改存储在 NSArray 中的 NSDictionary 键的值