循环遍历 NSDictionary 创建 NSArray

发布于 2025-01-07 07:22:29 字数 2048 浏览 1 评论 0原文

我有以下 NSDictionary 数据:

{
    "ADDED_DATE" = "2011-02-04 00:56:44.732014";
    "ADDED_LOGON" = ABCD;
    "AGENT_BROKER_IND" = N;
    "ALLOCATION_IND" = AUTOPROP;
    "BILLPRINT_DLVY_IND" = PAPER;
    "BILLPRINT_LOCATION_NAME" = "COMPANY ABC";
    "BILLPRINT_LVL_CIM" = 05852015;
    "BILL_DUE_DT" = "2011-02-01";
    "BILL_LOGO_CD" = XXLOGO;
    "BILL_PERIOD_MONTH" = 02;
    "BILL_PER_BEG_DT" = "2011-02-01";
    "BILL_PER_END_DT" = "2011-02-28";
    "BILL_RUN_DT" = "2011-02-03";
    "BILL_TOTAL_PREMIUM" = "342.84";
    IDX = ".000339709222474931";
    "LIST_BILL_CASE_NUM" = 0318T4;
    "LIST_BILL_CIM" = 05852019;
    "OWNING_CARRIER" = WX;
    "PAST_DUE_DT" = "2011-03-04";
    "REMIT_CYCLE" = MONTHLY;
    "RUN_NUMBER" = 1;
    "TOTAL_ADJUSTMENTS" = "0.00";
    "TOTAL_AMOUNT_DUE" = "685.68";
    "TOTAL_CURRENT_CHARGES" = "342.84";
    "TOTAL_PASTDUE_AMOUNT" = "342.84";
},
    {
    "ADDED_DATE" = "2010-12-04 08:20:45.292516";
    "AGENT_BROKER_IND" = N;
    "ALLOCATION_IND" = AUTOPROP;
    "BILLPRINT_DLVY_IND" = WEBPORTAL;
    "BILLPRINT_LOCATION_NAME" = "ANCHOR ABC";
    "BILLPRINT_LVL_CIM" = 05721991;
    "BILL_DUE_DT" = "2010-12-15";
    "BILL_FORM_TYPE_CD" = FE4;
    "BILL_LOGO_CD" = XXLOGO;
    "BILL_PERIOD_MONTH" = 01;
    "BILL_PER_BEG_DT" = "2011-01-01";
    "BILL_PER_END_DT" = "2011-01-31";
    "BILL_RUN_DT" = "2010-12-03";
    "BILL_TOTAL_PREMIUM" = "277.96";
    IDX = ".000389371998789428";
    "LIST_BILL_CASE_NUM" = 9858Q8;
    "LIST_BILL_CIM" = 05721991;
    "OWNING_CARRIER" = FE;
    "PAST_DUE_DT" = "2010-12-31";
    "REMIT_CYCLE" = MONTHLY;
    "RUN_NUMBER" = 1;
    "TOTAL_ADJUSTMENTS" = "0.00";
    "TOTAL_AMOUNT_DUE" = "277.96";
    "TOTAL_CURRENT_CHARGES" = "277.96";
    "TOTAL_PASTDUE_AMOUNT" = "0.00";
},

我正在尝试弄清楚如何将这些数据转换为数组。在这个例子中,我需要将它分解为 2 个数组,这些数组填充了它的数据(希望这是有意义的)。

我可以像这样循环数据:

NSString *key;

for (key in finalDict) {
    NSLog(@"%@", key);
}

我得到“key”中的每个数据“数组”......但是如何将它从那里分解成它自己的数组?抱歉,如果我感到困惑。 =)

感谢您的帮助!

I have the following NSDictionary data:

{
    "ADDED_DATE" = "2011-02-04 00:56:44.732014";
    "ADDED_LOGON" = ABCD;
    "AGENT_BROKER_IND" = N;
    "ALLOCATION_IND" = AUTOPROP;
    "BILLPRINT_DLVY_IND" = PAPER;
    "BILLPRINT_LOCATION_NAME" = "COMPANY ABC";
    "BILLPRINT_LVL_CIM" = 05852015;
    "BILL_DUE_DT" = "2011-02-01";
    "BILL_LOGO_CD" = XXLOGO;
    "BILL_PERIOD_MONTH" = 02;
    "BILL_PER_BEG_DT" = "2011-02-01";
    "BILL_PER_END_DT" = "2011-02-28";
    "BILL_RUN_DT" = "2011-02-03";
    "BILL_TOTAL_PREMIUM" = "342.84";
    IDX = ".000339709222474931";
    "LIST_BILL_CASE_NUM" = 0318T4;
    "LIST_BILL_CIM" = 05852019;
    "OWNING_CARRIER" = WX;
    "PAST_DUE_DT" = "2011-03-04";
    "REMIT_CYCLE" = MONTHLY;
    "RUN_NUMBER" = 1;
    "TOTAL_ADJUSTMENTS" = "0.00";
    "TOTAL_AMOUNT_DUE" = "685.68";
    "TOTAL_CURRENT_CHARGES" = "342.84";
    "TOTAL_PASTDUE_AMOUNT" = "342.84";
},
    {
    "ADDED_DATE" = "2010-12-04 08:20:45.292516";
    "AGENT_BROKER_IND" = N;
    "ALLOCATION_IND" = AUTOPROP;
    "BILLPRINT_DLVY_IND" = WEBPORTAL;
    "BILLPRINT_LOCATION_NAME" = "ANCHOR ABC";
    "BILLPRINT_LVL_CIM" = 05721991;
    "BILL_DUE_DT" = "2010-12-15";
    "BILL_FORM_TYPE_CD" = FE4;
    "BILL_LOGO_CD" = XXLOGO;
    "BILL_PERIOD_MONTH" = 01;
    "BILL_PER_BEG_DT" = "2011-01-01";
    "BILL_PER_END_DT" = "2011-01-31";
    "BILL_RUN_DT" = "2010-12-03";
    "BILL_TOTAL_PREMIUM" = "277.96";
    IDX = ".000389371998789428";
    "LIST_BILL_CASE_NUM" = 9858Q8;
    "LIST_BILL_CIM" = 05721991;
    "OWNING_CARRIER" = FE;
    "PAST_DUE_DT" = "2010-12-31";
    "REMIT_CYCLE" = MONTHLY;
    "RUN_NUMBER" = 1;
    "TOTAL_ADJUSTMENTS" = "0.00";
    "TOTAL_AMOUNT_DUE" = "277.96";
    "TOTAL_CURRENT_CHARGES" = "277.96";
    "TOTAL_PASTDUE_AMOUNT" = "0.00";
},

And I am trying to figure out how convert this data into arrays. With this example I need to have it broken down into 2 arrays with those arrays filled with it's data (hope that made sense).

I can loop through the data like this:

NSString *key;

for (key in finalDict) {
    NSLog(@"%@", key);
}

And I get each "array" of data in "key" ... but how to I break it down from there into it's own arrays? Sorry if I'm confusing. =)

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲸落 2025-01-14 07:22:30

所以,从你的代码来看,在我看来你有一个 NSDictionaries 的 NSArray 。如果您实际上想将所有数据加载到 UITableView 中,我建议您在 cellForRowAtIndexPath 方法中创建一个 NSDictionary 并使用当前索引 NSDictionary 加载它,如下所示:

NSDictionary *currentDictionary = [self.myArrayOfDictionaries objectAtIndex:indexPath.row];
    //now do some stuff with your dictionary
    cell.addedDate.text = [currentDictionary objectForKey:@"ADDED_DATE"];
    cell.agentBrokerInd.text = [currentDictionary objectForKey:@"AGENT_BROKER_IND"];
    ///etc.....

当然,这取决于您如何设置 UITableViewCell 以及是否或者你是否想要每个属性都有出口。

希望这有帮助。

So, it looks to me like you have an NSArray of NSDictionaries, judging by your code. If you actually want to load all the data into a UITableView I would suggest that in your cellForRowAtIndexPath method, you create an NSDictionary and load it with the current indexes NSDictionary like this:

NSDictionary *currentDictionary = [self.myArrayOfDictionaries objectAtIndex:indexPath.row];
    //now do some stuff with your dictionary
    cell.addedDate.text = [currentDictionary objectForKey:@"ADDED_DATE"];
    cell.agentBrokerInd.text = [currentDictionary objectForKey:@"AGENT_BROKER_IND"];
    ///etc.....

Of course, it depends on how you set up your UITableViewCell and whether or not you want to have outlets for each attribute or not.

Hope this helps.

不一样的天空 2025-01-14 07:22:30

key 设为 NSArray 类型,而不是 NSString。然后您可以访问数组的元素,将它们添加到另一个数组或您想要执行的任何其他操作。 这里是<代码> NSArray 文档。

Make key an NSArray type instead of NSString. Then you can access the array's elements, adding them to another array or whatever else you want to do. Here is the NSArray documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文