2010-08-15 53 views
0

我具有被從XML文件加載內部的數據結構對象時奇怪的輸出中的「沒有完成加載」委託的方法。試圖訪問數據

如果我使用的數據對象「printData」方法從委託的加載方法裏面顯示的所有信息。我可以多次這樣做。

不過,我有它運行在委託的第二種方法的按鈕,這一切都運行我的數據對象的printData方法,但我得到的是存在或是一些隨機屬性的文本。

例如「UIDeviceFamily」或「ar.lproj」等...

一旦這已打印崩潰該應用具有「EXC_BAD_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"); 
} 

這一切工作正常。

但是,當我然後運行相同的方法[myMap printClusters]從一個按鈕在代理中運行的方法,然後它打印第一行...「打印羣集for」。

然後怪行,然後崩潰。

+0

如果您可以發佈一些代碼,它會更好。內存管理問題,像往常一樣 – vodkhang 2010-08-15 15:35:39

+0

增加了一些代碼。 我希望它是有道理的。代碼遍歷集羣,然後遍歷集羣的每個首選項,然後遍歷每個首選項。 – Fogmeister 2010-08-15 15:56:22

回答

0

好了,遵循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; 
} 

嗚!

+0

接受答案的正確方法是......接受答案。 – 2010-08-16 02:04:58

+0

好吧,這在技術上並不是正確的答案,因爲實際的修復與您指定的行沒有任何關係,我從來不必使用任何保留屬性(事實上,它們不起作用)。但是,我確實投了票,因爲它有助於找到實際的修復方法,因爲它讓我沿着正確的方向思考。 – Fogmeister 2010-08-16 10:07:26

2

看來,clustersself屬性,但你永遠保留它。由於它是使用一種方便的方法創建的,該方法返回一個自動釋放的字典,它在方法範圍完成時被釋放。當你第二次打電話時,它不再活着。

變化:

clusters = [NSDictionary dictionaryWithDictionary:parser.clusters]; 

...到:

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

...或:

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

...,然後更改所有其他引用clusters到 。對於任何其他屬性也一樣。

+0

感謝您的提示! 你讓我想到了分配東西,而不是使用輸入變量,我找到了一些其他地方,並設法排序! 謝謝! – Fogmeister 2010-08-15 20:39:54