按顺序从 NSDictionary 获取所有键值 Objective C

Get all key values from NSDictionary in order Objective C

我需要获取字典的所有键及其值。

例如

我有这样的字典:

{
    "Last_Settled_Amount" = "7056.92";
    "Last_Settled_Date" = "2021-03-25T00:00:00";
    "Net_Settled_Amount" = "17810.23";
    "Settled_Adjustment_Amount" = "-79";
    "Settled_Tx_Amount" = 7202;
    "Settled_Tx_Fees" = "57.46";
    "Settled_Tx_VAT" = "8.619999999999999";
    "Total_Payment_Count" = 3;
    "Total_Settled_Tx" = 22;
    "Transfer_Charge" = 0;
}

我想获取它的键值所以我这样实现

for (NSString* key in [dd allKeys]) {

        id value = [dd objectForKey:key];
        NSLog(@"areacodedisplay: value = %@",value);
        NSLog(@"areacodedisplay: key = %@",key);
   }

我可以获取所有的键和值,但顺序不对。我想要和我在字典里找到的顺序一样的顺序。

字典是无序的,但您似乎希望键按字母顺序排列,因此您可以对 allKeys 数组进行排序。

for (NSString* key in [[dd allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
    id value = [dd objectForKey:key];
    NSLog(@"areacodedisplay: value = %@",value);         
    NSLog(@"areacodedisplay: key = %@",key);
}

如果您希望这些值具有特定但任意的顺序,那么您需要求助于直接访问每个值。