【问题标题】:Creating an array content with dictionaries using NSXMLParsing in iOS在 iOS 中使用 NSXMLParsing 创建带有字典的数组内容
【发布时间】:2013-07-02 05:39:18
【问题描述】:

下午的狂欢,

在我的代码中,我试图解析 NSData 格式的 SOAP 响应。使用这些数据,使用 NSXMLParse,我正在尝试创建一个字典数组。问题是,每当我将新字典添加到数组中时,它都会用当前添加的字典对象内容替换旧字典对象内容。例如,在解析结束时,我的数组中有 7 个具有相同内容的字典。这是显示我所做的代码;

- (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];
        }
    }
}

提前谢谢你。 问候

【问题讨论】:

    标签: iphone ios objective-c parsing nsxmlparser


    【解决方案1】:

    当您调用 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];
    }
    

    【讨论】:

      【解决方案2】:

      我认为您期望每个 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];
      }
      

      【讨论】:

        【解决方案3】:

        代替:

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

        试试:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-18
          • 2016-03-25
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多