【问题标题】:Strange NSXMLParser Issue奇怪的 NSXMLParser 问题
【发布时间】:2014-04-05 15:25:03
【问题描述】:

我创建了一个应用程序,它几乎是 Apple 的 SeismicXML 示例应用程序的克隆,以便我可以了解 NSXMLParser 的细节。我创建了自己的.xml 文件,并将大部分(如果不是全部)代码从 SeismicXML 应用程序复制到我的名为 XMLTest 的应用程序中。除了一部分之外,一切似乎都运行良好。当应用程序运行时,所有文本字段都显示相同的数据。无论我设置textField.text显示的解析的哪个属性,它都显示相同的数据,恰好是最后一个被解析的元素。

在调试过程中,我进入了解析操作,并实现了许多NSLog 语句,以查看正在解析的数据。所有NSLog 语句都显示正在解析.xml 文件的正确元素并将其设置为正确对象的正确属性。

我不确定我的代码的哪一部分是错误的,而且我已经自己调试这个应用程序太多小时了。我已经附上了我认为可能是问题的罪魁祸首的部分代码。

在这段代码 sn-p 中,两个标签的值是相同的,这是不应该的。

@implementation VTSVehicleTableViewCell

-(void)configureWithVehicle:(VTSVehicle *)vehicle {

self.stockLabel.text = vehicle.stock;
self.infoLabel.text = vehicle.year;

}

这是在模拟器中生成的:

但是,就在这个方法内部,它在控制台中生成了以下正确信息:

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

if (_accumulatingParsedCharacterData) {
    // If the current element is one whose content we care about, append 'string'
    // to the property that holds the content of the current element.
    //
    NSLog(@"%@", string);
    [self.currentParsedCharacterData appendString:string];
    NSLog(@"%@", string);
}
}

就像车辆对象在解析 xml 期间和之后在其中包含正确的数据,但是当我去更新视图时,车辆对象中的数据现在只填充最后一个内部的数据xml 文件的元素。这是 xml 文件的样子:

<vehicles published="2014-04-02">
<vehicle>
    <stock>003737</stock>
    <serial>WVWDM7AJ2BW166848</serial>
    <year>2011</year>
    <make>VOLKSWAGEN</make>
    <model>GOLF</model>
    <date>01/05/11</date>
    <price>26540.00</price>
    <invoice>25414.00</invoice>
    <trim>4DR TDI 6SP AUT</trim>
</vehicle>
<vehicle>
    <stock>003974</stock>
    <serial>3VWLL7AJ1CM317028</serial>
    <year>2012</year>
    <make>VOLKSWAGEN</make>
    <model>JETTA</model>
    <date>11/03/11</date>
    <price>0.00</price>
    <invoice></invoice>
    <trim>2.0L TDI 6SP AUTO</trim>
</vehicle>
<vehicle>
    <stock>004104</stock>
    <serial>1VWBH7A38CC061891</serial>
    <year>2012</year>
    <make>VOLKSWAGEN</make>
    <model>PASSAT</model>
    <date>03/05/12</date>
    <price>26665.00</price>
    <invoice>25555.00</invoice>
    <trim>SE W/SR 2.5 6SP AUTO</trim>
</vehicle>
</vehicles>

为什么文本标签显示相同的数据,即使它们来自同一对象的不同属性?

编辑:这是cellForRowAtIndexPath的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *kVehicleCellID = @"VehicleCellID";
VTSVehicleTableViewCell *cell = (VTSVehicleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kVehicleCellID];

// Get the specific vehicle for this row.
VTSVehicle *vehicle = (self.vehicleList)[indexPath.row];

[cell configureWithVehicle:vehicle];

return cell;
}

这是tableViewCell的代码:

#import "VTSVehicleTableViewCell.h"
#import "VTSVehicle.h"

@interface VTSVehicleTableViewCell ()

// References to the subviews which display the earthquake data.
@property (nonatomic, weak) IBOutlet UILabel *stockLabel;
@property (nonatomic, weak) IBOutlet UILabel *infoLabel;

@end

@implementation VTSVehicleTableViewCell

-(void)configureWithVehicle:(VTSVehicle *)vehicle {

    self.stockLabel.text = vehicle.stock;
    self.infoLabel.text = vehicle.year;

}

@end

【问题讨论】:

  • 我怀疑您被否决了,因为您将代码放在了外部站点上。请编辑您的问题以在您的问题中包含相关部分。请参阅 Stack Overflow 帮助系统的 How do I ask a good question 部分的“如何帮助他人重现您的问题”,其中指出您应该在问题中包含代码示例。
  • 谢谢@Rob。我编辑了我的问题。
  • 会不会是你解析得很好但实现 UITableView(和/或其模型数据)错误?
  • 问题可能出在 cellForRowAtIndexPath 中,正如 matt 建议的那样,或者更有可能出现在 didEndElementdidStartElement 中,重复使用您的 NSMutableString,而忽略了 copy 的值。你能在那些初始化和/或保存currentParsedCharacterData的方法中向我们展示相关的sn-ps吗?
  • 我已经添加了来自 cellForRowAtIndexPath 的代码以供参考。

标签: objective-c xml parsing nsxmlparser


【解决方案1】:

问题不在foundCharacters,而更可能在didStartElementdidEndElement。可能发生的情况是您有一个 NSMutableString 实例,您对每个元素一次又一次地使用它,然后在模型中使用该元素值。

当您将currentParsedCharacterData 的结果保存在didEndElement 中时,您应该确保您正在复制该值(不仅仅是使用对现有NSMutableString 的引用。例如,您可以使用@987654328 @ 属性定义为 copy 属性,而不是 strong 属性。或者,如果您只是将其放入字典或数组中,请使用 [self.currentParsedCharacterData copy] 而不仅仅是 currentParsedCharacterData。这将确保每个元素您保存在模型中的最终结果是一个单独的 NSString 实例(顺便说一句,确保这些对象不再可变)。

显然,正如 matt 所建议的,您的cellForRowAtIndexPath 中可能存在一个简单的问题,但我怀疑NSXMLParserDelegate 代码更有可能是罪魁祸首。

【讨论】:

  • 您对使用 [self.currentParsedCharacterData copy] 的建议解决了我遇到的问题。感谢你的回答!!!!非常感谢。
猜你喜欢
  • 2011-01-17
  • 2012-01-19
  • 1970-01-01
  • 2014-07-26
  • 2015-02-27
  • 2018-06-26
  • 2011-08-13
  • 2011-08-17
  • 2011-02-02
相关资源
最近更新 更多