字典数组

时间:2011-08-10 21:28:37

标签: iphone objective-c xcode

我有NSMutableArraypurDesc,其中包含NSMutableDictionarypurDetails。当我使用NSLog(@"%@", purDesc);时,它会返回空格,其中应该有dictionarys,如下所示:

singleton purDesc Dump:(
        {
    },
        {
    },
        {
    },
        {
    },
        {
    }
)

现在每次添加字典时,size / look / dump都会更改,以反映添加/删除字典的次数。以下是从我的XML解析器中获取的代码片段:

...

else if ([elementName isEqualToString:@"purDesc"])
{
    //close purDisc
    // dumps dictionary into log
    NSLog(@"End of purDesc");
    NSLog(@"Dump:%@", [purDesc description]);

    [singleton setPurDesc:purDesc];//sets this response to the response in the singleton

    NSLog(@"singleton purDesc Dump:%@", [[singleton purDesc] description]);
    return;//we're done here
}
else if ([elementName isEqualToString:@"PurDetails"])
{
    //close purDetails
    // dumps dictionary into log
    NSLog(@"End of PurDetails");
    [purDetails addEntriesFromDictionary:description];//sets current purDetails to current description
    NSLog(@"Dump:%@", [purDetails description]);

    [purDesc addObject:purDetails];//adds purDetails dictionary to purDesc
    [purDetails removeAllObjects];
    NSLog(@"Current purDesc:%@", [purDesc description]); 
}

我做错了什么,为什么日志会半空?

1 个答案:

答案 0 :(得分:3)

我看到你这样做:

[purDesc addObject:purDetails];//adds purDetails dictionary to purDesc
[purDetails removeAllObjects];

引用 存储到 purDesc 中的 purDetails ,并增加其保留计数,但 不制作副本 。因此,如果您 removeAllObjects ,则字典的内容将丢失。您在 purDesc 中“存储”对它的引用的事实不会改变它。我想你看到的是这个或类似动作的结果。

您无法重复使用字典。您每次都必须分配并初始化一个新的。您应该释放您拥有的本地,但不能清除它。

我需要查看更多代码,了解如何改进设计,这样就不会出现问题,并且内存管理得到妥善处理。

相关问题