我想解析一个XML文件,我通过URL获得,然后我解析XML的每个元素,并将其添加到NSMutableDictionary中,调用objets一切顺利,但是当我尝试将myMutableDictionnary添加到myMutableArray时,使用什么也没有发生......将NSMutableDictionary添加到NSMutableArray
[myMutableArray计数]返回0 & [myMutableDictionary计数]返回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];
和现在的一切工作正常。 谢谢你们的帮助!
你不表明inits阵列的任何代码。它存在吗?尝试记录'myMutableArray'来验证它不是零。 – 2012-04-24 13:05:45
是的,它存在! – Hosni 2012-04-24 13:08:22
你是什么意思通过尝试日志记录? – Hosni 2012-04-24 13:12:22