Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 奇怪的NSXMLParser问题_Objective C_Xml_Parsing_Nsxmlparser - Fatal编程技术网

Objective c 奇怪的NSXMLParser问题

Objective c 奇怪的NSXMLParser问题,objective-c,xml,parsing,nsxmlparser,Objective C,Xml,Parsing,Nsxmlparser,我创建了一个应用程序,它几乎是苹果SeismicXML示例应用程序的克隆,这样我就可以了解NSXMLParser的细节。我创建了自己的.xml文件,并将SeismicXML应用程序中的大部分(如果不是全部的话)代码复制到名为XMLTest的应用程序中。除了一部分之外,一切似乎都很顺利。当应用程序运行时,所有文本字段显示相同的数据。无论我将textField.text设置为显示解析的哪个属性,它都显示相同的数据,这恰好是解析的最后一个元素 在调试期间,我进行了解析操作,并实现了许多NSLog语句,

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

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

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

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

@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期间和之后,vehicle对象中包含正确的数据,但是当我去更新视图时,vehicle对象中的数据现在只填充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>
以下是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

问题不在于
foundCharacters
,而更可能在于
didStartElement
didEndElement
。可能发生的情况是,您有一个
NSMutableString
实例,对每个元素反复使用,然后在模型中使用该元素值

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


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

我怀疑您被否决了,因为您将代码放在了外部网站上。请编辑您的问题,将相关部分包括在您的问题中上。请参阅堆栈溢出帮助系统部分的“如何帮助他人重现您的问题”,其中指出您应该在问题中包含代码示例。谢谢@Rob。我编辑了我的问题。是否您解析得很好,但实现了UITableView(和/或其模型数据)错误?正如matt所建议的,问题可能出现在
cellForRowAtIndexPath
中,或者更可能出现在
didEndElement
didStartElement
中,重用
NSMutableString
,忽略了
复制
值。您能否在初始化和/或保存
c的方法中向我们展示相关片段urrentParsedCharacterData
?我已经添加了cellForRowAtIndexPath中的代码以供参考。您建议使用[self.currentParsedCharacterData copy]修复了我遇到的问题。感谢您的回答!!!!非常感谢。
#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