0

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

当我单步执行代码时,每个NSDictionary都正确创建并包含预期值......我通过以下方式访问这个数组(填充在我的模型中):

@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++;
}
4

1 回答 1

2

我觉得这条线很烂。 NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];

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

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

于 2013-02-19T00:26:10.410 回答