我有一系列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++;
}
答案 0 :(得分:2)
我认为这条线太糟糕了。
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];
在将removeAllObjects
添加到字典后,您删除了所有对象,因此删除了它自己的对象。尝试将该行更改为
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:arrayOfObjects] forKey:[idArray lastObject]];