1

Afternoon overflowers,

In my code, I'm trying to parse and SOAP response that is in NSData format. With that data, using NSXMLParse, I'm trying to create an array of dictionaries. The problem is, anytime I add my new dictionary to the array, it replaces the old dictionary object contents with the currently added one. For instance, at the end of the parse, I have 7 dictionaries with the same content in my array. Here is the code to show what I have done;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Received the SOAP data.");

    if (!itemArray) {
        itemArray = [[NSMutableArray alloc] init];
    }
    else {
        itemArray = nil;
        itemArray = [[NSMutableArray alloc] init];
    }

    if (!itemDictionary) {
        itemDictionary = [[NSMutableDictionary alloc] init];
    }
    else {
        itemDictionary = nil;
        itemDictionary = [[NSMutableDictionary alloc] init];
    }

    parser = [[NSXMLParser alloc] initWithData:webData];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started Element %@", elementName);
    element = [NSMutableString string];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(element == nil) {
        element = [[NSMutableString alloc] init];
    }

    [element appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"Found an element named: %@ with a value of: %@", elementName, element);

    if (![elementName isEqualToString:@"item"]) {
        [itemDictionary setValue:element forKey:elementName];
    }
    else if ([elementName isEqualToString:@"item"]) {
        if (itemDictionary) {
            [itemArray addObject:itemDictionary];

            [itemDictionary removeAllObjects];
        }
    }
}

Thank you in advance. Regards

4

3 回答 3

0

我认为您期望每个 itemDictionary 都是需要存储在 itemArray 中的单个对象。因此,一旦启动元素,就必须创建字典实例。试试这样

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started Element %@", elementName);
    itemDictionary = [[NSMutableDictionary alloc] init];
    element = [NSMutableString string];
}
于 2013-07-04T09:39:03.347 回答
0

当您调用 removeAllObjects 时,它只使用您的数组正在添加的同一个字典实例。当你修改你的字典时,它会修改你的数组包含的同一个字典(因为两者实际上是相同的),所以不是调用 removeAllObjects,而是在每次新元素开始时创建新字典。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started Element %@", elementName);
    itemDictionary = [[NSMutableDictionary alloc] init];
    element = [NSMutableString string];
}
于 2013-07-04T10:09:30.597 回答
0

代替:

[itemArray addObject:itemDictionary];
[itemDictionary removeAllObjects];

尝试:

[itemArray addObject:itemDictionary];
itemDictionary = [[NSMutableDictionary alloc] init];

因此,您无需从字典中删除所有项目,而是为下一组内容创建一个新字典。

于 2013-07-04T09:27:50.407 回答