这与我之前的问题类似。我没有得到答案,也许是通过改变我可能得到答案的问题。
这是我的解析代码:
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
if ([elementName isEqualToString:kimgurl]
|| [elementName isEqualToString:kone_x]
|| [elementName isEqualToString:kone_y]
|| [elementName isEqualToString:kone_radius]
|| [elementName isEqualToString:ktwo_x]
|| [elementName isEqualToString:ktwo_y]
|| [elementName isEqualToString:ktwo_radius])
{
elementFound = YES;
theItems = [[Items alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:kimgurl])
{
theItems.imageURL = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_x])
{
theItems.iOne_X = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_y])
{
theItems.iOne_Y = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_radius])
{
theItems.iOne_Radius = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_x])
{
theItems.iTwo_X = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_y])
{
theItems.iTwo_Y = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_radius])
{
theItems.iTwo_Radius = self.currentValue;
[self.currentValue setString:@""];
}
}
-(void) parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"enddocument: %@", theItems.imageURL);
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound == YES) {
if(!currentValue)
{
currentValue = [NSMutableString string];
}
[currentValue appendString: string];
}
}
当我到达parserDidEndDocument时。 theItems类是空的。
这是Items.h
#import <Foundation/Foundation.h>
@interface Items : NSObject {
@private
//parsed data
NSString *imageURL;
NSString *iOne_X;
NSString *iOne_Y;
NSString *iOne_Radius;
NSString *iTwo_X;
NSString *iTwo_Y;
NSString *iTwo_Radius;
}
@property (nonatomic, retain) NSString *imageURL;
@property (nonatomic, retain) NSString *iOne_X;
@property (nonatomic, retain) NSString *iOne_Y;
@property (nonatomic, retain) NSString *iOne_Radius;
@property (nonatomic, retain) NSString *iTwo_X;
@property (nonatomic, retain) NSString *iTwo_Y;
@property (nonatomic, retain) NSString *iTwo_Radius;
@end
这里是Items.m
#import "Items.h"
@implementation Items
@synthesize imageURL;
@synthesize iOne_X;
@synthesize iOne_Y;
@synthesize iOne_Radius;
@synthesize iTwo_X;
@synthesize iTwo_Y;
@synthesize iTwo_Radius;
-(void)dealloc
{
[imageURL release];
[iOne_X release];
[iOne_Y release];
[iOne_Radius release];
[iTwo_X release];
[iTwo_Y release];
[iTwo_Radius release];
[super dealloc];
}
@end
这是我的RootViewController.h
#import <UIKit/UIKit.h>
@class Items;
@interface RootViewController : UIViewController <NSXMLParserDelegate> {
NSMutableData *downloadData;
NSURLConnection *connection;
BOOL elementFound;
NSMutableString *currentValue;
NSMutableDictionary *pictures;
//---xml parsing---
NSXMLParser *xmlParser;
Items *theItems;
NSMutableArray *aItems;
}
@property (nonatomic, retain) Items *theItems;
@property (nonatomic, retain) NSMutableArray *aItems;
@property (nonatomic, retain) NSMutableString *currentValue;
@property (nonatomic, retain) NSMutableData *downloadData;
@property (nonatomic, retain) NSURLConnection *connection;
@end
xml文件示例
<?xml version="1.0" encoding="utf-8"?>
<data>
<test>
<url>url</url>
<one_x>83</one_x>
<one_y>187</one_y>
<one_radius>80</one_radius>
<two_x>183</two_x>
<two_y>193</two_y>
<two_radius>76</two_radius>
</test>
</data>
答案 0 :(得分:1)
看起来有几个潜在的问题。在你的didStartElement方法中,你为每个元素分配/初始化一个新的Items对象并覆盖你以前的元素。也许您可以将Items init移动到-parserDidStartDocument:方法中。当你初始化时,它应该看起来更像这样:
Items * items = [[Items alloc] init]; self.theItems = items; [项目发布];
完成后,您将获得正确的保留计数。
我还建议将NSString @property声明更改为copy而不是retain。代码:
theItems.imageURL = self.currentValue;
[self.currentValue setString:@""];
......没有按照你的想法行事。 theItems.imageURL将指向你的NSMutableString然后你清除可变字符串,之后意味着imageURL指向一个空的可变字符串。然后在所有其他迭代之后,所有迭代都指向相同的NSMutableString,它是空的。如果你将@property声明更改为copy,那么它会将imageURL设置为self.currentValue内容的不可变NSString副本。