Iphone 尝试访问数据时出现奇怪的输出

Iphone 尝试访问数据时出现奇怪的输出,iphone,exc-bad-access,Iphone,Exc Bad Access,我有一个数据结构对象,它是从委托的“did finish load”方法中的XML文件加载的 如果我在委托的load方法中使用data objects“printData”方法,它会显示所有信息。我可以做很多次 但是,我有一个按钮,它在委托中运行第二个方法,它所做的就是运行我的数据对象的printData方法,但我所得到的要么是nothing,要么是一些随机属性文本 e、 g.“UIDeviceFamily”或“ar.lproj”等 一旦打印出来,应用程序就会崩溃,出现“EXC\u BAD\u

我有一个数据结构对象,它是从委托的“did finish load”方法中的XML文件加载的

如果我在委托的load方法中使用data objects“printData”方法,它会显示所有信息。我可以做很多次

但是,我有一个按钮,它在委托中运行第二个方法,它所做的就是运行我的数据对象的printData方法,但我所得到的要么是nothing,要么是一些随机属性文本

e、 g.“UIDeviceFamily”或“ar.lproj”等

一旦打印出来,应用程序就会崩溃,出现“EXC\u BAD\u ACCESS”错误

我记得有一个目录需要查找,以查找此错误的更多详细信息,但我不记得是哪个目录

谢谢你能提供的帮助

抱歉,这是应用程序中重要的代码

这是填充数据的代码

    -(id)initMapFromXMLData:(NSData *)xmlData
{
    if (self = [super init])
    {
        MapXMLParser *parser = [[MapXMLParser alloc] initWithData:xmlData];
        [parser setDelegate:parser];
        [parser parse];

        statementPairs = [NSDictionary dictionaryWithDictionary:parser.statementPairs];
        prefPairs = [NSDictionary dictionaryWithDictionary:parser.prefPairs];
        clusters = [NSDictionary dictionaryWithDictionary:parser.clusters];

        /*
        statementPairs = parser.statementPairs;
        prefPairs = parser.prefPairs;
        clusters = parser.clusters;
        [parser release];*/
    }
    return self;
}
然后从委托运行此操作

NSData *xmlData;

xmlData = [[NSData alloc] initWithContentsOfFile:@"/Users/oliver/Documents/XCode stuff/Saviio/Saviio 2/Saviio/Classes/Statements"];

myMap = [[Map alloc] initMapFromXMLData:xmlData];
然后在同一个委托方法中显示它

[myMap printClusters];
。。。它运行

-(void)printClusters
{
    NSLog(@"Printing Clusters for %@", self);

    for (int i = 1; i <= [clusters count]; i++)
    {
        Cluster *tempCluster;

        tempCluster = [clusters objectForKey:[NSString stringWithFormat:@"%d", i]];

        NSLog(@"Cluster %@", tempCluster.name);

        for (int j = 0 ; j < [tempCluster.fgids count]; j++)
        {
            Preference *tempPref;

            tempPref = [prefPairs objectForKey:[[tempCluster.fgids objectAtIndex:j] stringValue]];

            NSLog(@"Pref ---> %@/%@", tempPref.left, tempPref.right);

            for (int k = 0 ; k < [tempPref.qids count]; k++)
            {
                Statement *tempStat;

                tempStat = [statementPairs objectForKey:[[tempPref.qids objectAtIndex:k] stringValue]];

                NSLog(@"Stat -------> %@ - %@", tempStat.left, tempStat.right);
            }
        }
    }

    NSLog(@"END");
}
-(无效)打印群集
{
NSLog(@“打印群集为%@”,self);

对于(int i=1;i而言,
clusters
似乎是
self
的一个属性,但您从未保留它。由于它是使用一个方便的方法创建的,该方法返回一个自动删除的字典,因此在方法作用域完成时将其释放。当您第二次调用它时,它不再处于活动状态

更改:

clusters = [NSDictionary dictionaryWithDictionary:parser.clusters];
…致:

self.clusters = [NSDictionary dictionaryWithDictionary:parser.clusters];
…或:

clusters = [[NSDictionary dictionaryWithDictionary:parser.clusters] retain];

…然后将对
clusters
的所有其他引用更改为
self.clusters
。对任何其他属性执行相同操作。

好吧,按照TechZen的建议,我查看了填充NSDictionary的代码,发现我正在存储指向的解析器变量

相反,我将它们更改为分配自己的变量并存储它们

那就对了

旧代码

-(id)initWithName:(NSString *)inputName
{
    if (self = [super init])
    {
        name = inputName;
        fgids = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return self;
}
新代码

-(id)initWithName:(NSString *)inputName
{
    if (self = [super init])
    {
        self.name = [NSString stringWithString:inputName];
        self.fgids = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return self;
}

Woo!

如果你能发布一些代码会更好。内存管理问题,就像通常添加的一些代码一样。我希望这是有意义的。代码通过集群,然后通过集群的每个首选项,然后通过首选项的每个语句。谢谢你的提示!你让我考虑分配内容,而不是使用输入变量,然后我找到了一些其他地方并对其进行了排序!谢谢!接受答案的正确方法是…接受答案。从技术上讲,这不是正确的答案,因为实际的修复与您指定的行无关,而且我从未使用过任何保留属性(事实上,它们不起作用).不过,我还是投了赞成票,因为这有助于找到实际的解决方案,因为它让我的思路正确。