将对象添加到NSMutableArray时,从XML文件解析时,顺序似乎很奇怪

时间:2009-05-07 18:28:13

标签: iphone xml nsmutablearray

我正在解析两个元素的XML文件:“title”和“noType”。解析完这些后,我将它们添加到一个名为aMaster的对象中,这是我自己的Master类的一个实例,它包含NSString变量。

然后我将这些实例添加到单例的NSMutableArray上,以便在程序的其他地方调用它们。问题是当我调用它们时,它们似乎不在同一个NSMutableArray索引上......每个索引都包含标题或noType元素,当它应该是两者时......任何人都可以看到我可能是什么做错了?下面是解析器的代码。非常感谢!!

#import "XMLParser.h"
#import "Values.h"
#import "Listing.h"
#import "Master.h"

@implementation XMLParser

@synthesize sharedSingleton, aMaster;

- (XMLParser *) initXMLParser {

    [super init];
    sharedSingleton = [Values sharedValues];
    aMaster = [[Master init] alloc];
    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    aMaster = [[Master alloc] init];

        //Extract the attribute here.

    if ([elementName isEqualToString:@"intro"]) {
        aMaster.intro = [attributeDict objectForKey:@"enabled"];
    } else if ([elementName isEqualToString:@"item"]) {
        aMaster.item_type = [attributeDict objectForKey:@"type"];
        //NSLog(@"Did find item with type %@", [attributeDict objectForKey:@"type"]);

        //NSLog(@"Reading id value :%@", aMaster.item_type);
    } else {
        //NSLog(@"No known elements");
    }

    //NSLog(@"Processing Element: %@", elementName);    //HERE
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else {
        [currentElementValue appendString:string];//[tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        CFStringTrimWhitespace((CFMutableStringRef)currentElementValue);
    }
}

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

    if ([elementName isEqualToString:@"item"]) {
        [sharedSingleton.master addObject:aMaster];
        NSLog(@"Added %@ and %@ to the shared singleton", aMaster.title, aMaster.noType);  //Only having one at a time added... don't know why
        [aMaster release];

        aMaster = nil;

    } else if ([elementName isEqualToString:@"title"]) {
        [aMaster setValue:currentElementValue forKey:@"title"];
    } else if ([elementName isEqualToString:@"noType"]) {
        [aMaster setValue:currentElementValue forKey:@"noType"]; 
        //NSLog(@"%@ should load into the singleton", aMaster.noType);
    }
    NSLog(@"delimiter");
    NSLog(@"%@ should load into the singleton", aMaster.title);
    NSLog(@"%@ should load into the singleton", aMaster.noType);

    [currentElementValue release];
    currentElementValue = nil;

}

- (void) dealloc {
    [aMaster release];
    [currentElementValue release];
    [super dealloc];
}

@end

1 个答案:

答案 0 :(得分:0)

每次调用didStartElement时,您都会将aMaster设置为Master类的新实例。基于didEndElement的实现,看起来您应该只在找到新的item标记时创建新实例。这可能就是为什么数组中的每个条目都有一个值或另一个值,因为为每个值创建了一个新实例。