我的NSDictionaries数组只返回键,没有值。我怎样才能解决这个问题?

时间:2013-02-18 08:05:16

标签: objective-c nsmutablearray nsdictionary

我有一系列NSDictionaries。现在,为简单起见,这个数组只包含两个字典。

当我逐步完成代码时,每个NSDictionary都已正确创建,包含预期值 ...我通过以下方式访问此数组(在我的模型中填充): / p>

@property (nonatomic, retain) NSMutableArray *sDictionaries;

我通过以下方式在我的控制器中访问此属性:

NSArray *array = [[NSArray alloc]initWithArray:sLocator.sDictionaries]但是当我查看array时,它只包含键和值(我在模型中创建字典时已经看到了) )。

非常感谢将此NSDictionary个对象从我的模型传递到控制器的任何帮助!

编辑:这是我迭代一些收到的数据并将其放在NSDictionary个对象中的代码。

NSMutableArray *arrayOfObjects = [[NSMutableArray alloc]initWithCapacity:0];
sDictionaries = [[NSMutableArray alloc]initWithCapacity:0];

int i = 0;
//iterate through each element, get the string, then add to the array
//I know for this example I am receiving 10 element objects which are NSStrings. 
//The first string is the key of the dictionary and the next 4 strings are the values.
//Then all the objects are removed from arrayOfObjects and a new dictionary is made
for(Element *element in nodes)
{
    [mArray addObject:[element content]];

    if(i%5 != 0)
    {
        [arrayOfObjects addObject:[element content]];
    }

    if(i%5 == 0)
    {   
        [idArray addObject:[element content]];
    }

    if([arrayOfObjects count] >= 4)
    {
       //Here is where the dictionary is created then added to the array. 
        NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];
        [sDictionaries addObject:dictionary];

        [arrayOfObjects removeAllObjects];
    }

    i++;
}

1 个答案:

答案 0 :(得分:2)

我认为这条线太糟糕了。 NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];

在将removeAllObjects添加到字典后,您删除了所有对象,因此删除了它自己的对象。尝试将该行更改为

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:arrayOfObjects] forKey:[idArray lastObject]];