0

我正在尝试解析通过 URL 获取的 XMLfile,然后解析我的 XML 的每个元素并将其添加到一个名为 objets 的 NSMutableDictionary 中,一切顺利,但是当我尝试将 myMutableDictionnary 添加到 myMutableArray 时没有任何反应...

[myMutableArray count] 返回 0 & [myMutableDictionary count] 返回 3 //这是正确的

这是我声明变量的接口:

@protocol XMLParserDelegate;//déclaration du delegate

@interface XMLParser : NSObject <NSXMLParserDelegate>// on adopte le délégate   NSXMLParserDelegate pour la classe
{
NSXMLParser *monParseur;
NSMutableArray *myMutablArray;

NSMutableDictionary *myMutableDictionary;

NSString *objetCourrant;
NSMutableString *journalCourrant;
NSMutableString *dateCourrant;
NSMutableString *descriptionCourrant;

id<XMLParserDelegate> delegate; //on déclare le délégate sous le nom de delegate
}
-(void)parseXMLByPath:(NSString*)chemin;

@property (nonatomic,retain) NSMutableArray *maTabledeJournals;
@property  (nonatomic,retain) id<XMLParserDelegate> delegate;

@end
@protocol XMLParserDelegate <NSObject>
-(void) parsingIsFinished;
@end

这是我定义解析器的地方:

-(void)parserDidStartDocument:(NSXMLParser *)parser
{
 myMutableArray=[[NSMutableArray alloc] init];
NSLog(@"Started");
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

if (objetCourrant) {
    objetCourrant=nil;
}    
objetCourrant=[elementName copy];

if([elementName isEqualToString:kJournal ])
{
    if(myMutableDictionary)
    {
        myMutableDictionary=nil;
    }
    if(journalCourrant)
    {
        journalCourrant=nil;
    }
    if(dateCourrant)
    {
        dateCourrant=nil;
    }
    if(descriptionCourrant)
    {
        descriptionCourrant=nil;
    }
    
    myMutableDictionary = [[NSMutableDictionary alloc] init];
    

    journalCourrant = [[NSMutableString alloc] init];
    dateCourrant = [[NSMutableString  alloc] init];
    descriptionCourrant = [[NSMutableString alloc] init];
    
    NSLog(@"is parsing...");
}

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  {

if([elementName isEqualToString:kJournal])
{
   
    [myMutableDictionary setObject:journalCourrant forKey:kTitre]; NSLog(@"%@",journalCourrant);
    [myMutableDictionary setObject:dateCourrant forKey:kDate];  NSLog(@"%@",dateCourrant);
    [myMutableDictionary setObject:descriptionCourrant forKey:kDescription]; NSLog(@"%@",descriptionCourrant);
    
    NSLog(@"my dictionary %i",[myMutableDictionary count]);
    
    [myMutableArray addObject:myMutableDictionary];
    
    NSLog(@"my array %i",[myMutableArray count]);
}

  }

知道什么可能在这里造成麻烦吗?

解决方案

而不是将 myMutableDictionary 添加到 myMutableArray 中,如下所示:

[myMutableArray addObject:myMutableDictionary];

我做了:

[self.myMutableArray addObject:myMutableDictionary];

现在一切正常。谢谢你们的帮助!

4

2 回答 2

2

您的代码不起作用的原因是您没有初始化可变数组。

添加

myMutableArray = [[NSMutableArray alloc] init];

初始化变量的地方

于 2012-04-24T13:06:15.227 回答
0

好吧,我已经阅读了其他答案和评论,看来您仍然遇到问题,使其工作的最基本代码如下:

NSMutableArray * myArray = [[NSMutableArray alloc] init];
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
[myArray addObject:myDict];
NSLog(@"Count = %i", [myArray count]); // returns 1

您可以发布@interface到您声明 myMutableArray 变量的位置吗?

此外,最好不要在您拥有的同一行上调用两个函数/方法:

[myMutableDictionary setObject:journalCourrant forKey:kTitre]; NSLog(@"%@",journalCourrant);
[myMutableDictionary setObject:dateCourrant forKey:kDate];  NSLog(@"%@",dateCourrant);
[myMutableDictionary setObject:descriptionCourrant forKey:kDescription]; NSLog(@"%@",descriptionCourrant);
于 2012-04-24T13:30:20.707 回答