【问题标题】:Split an array of dictionaries into n array of dictionaries in objective c将一个字典数组拆分为目标 c 中的 n 个字典数组
【发布时间】:2014-06-23 09:57:33
【问题描述】:

我有一个包含 n 个字典的数组,并希望将其拆分为包含在数组中的 x 个元素的 n 个数组。

input : n 个元素的数组 输出:由 x 个元素组成的 n 个数组组成的数组

暂时我这样做了:

    NSMutableArray *contacts = [[NSMutableArray alloc] init];
    NSMutableArray *test = [[NSMutableArray alloc] init];
    int counter = 0;
    for (id contact in pfContacts)
    {
        counter++;
        [test addObject:contact];
        if (counter%80 == 0 || counter == [pfContacts count])
        {
            int index = counter/81;
            [contacts insertObject:test atIndex:index];
            [test removeAllObjects];
        }
    }

但我得到一个空数组的数组

【问题讨论】:

  • 你的问题到底是什么?
  • 展示你的尝试并解释它做错了什么。
  • 你能描述一下你遇到了什么问题吗?
  • 在编辑中查看我的代码
  • 使用字典数组而不是自定义类对象真的不清楚。尝试重构您的代码,它可能会简化您的问题

标签: ios objective-c arrays sorting nsdictionary


【解决方案1】:

你遇到了这个问题

if (counter%80 == 0 || counter == [pfContacts count])
        {
            int index = counter/81;
            [contacts insertObject:test atIndex:index];
            [test removeAllObjects];
        }

因为如果计数器等于 80 而不是 if 条件将起作用,但在这种情况下你正在使用这个

int index = counter/81;

它的意思

 int index = counter/81;  && 80/81 =  0.98

所以它正在出错.. 改成

int index = counter/80; 

再试一次……可能会有帮助……

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2021-04-15
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2013-03-12
    • 2021-01-21
    • 2021-12-08
    相关资源
    最近更新 更多