gpt4 book ai didi

iphone - NSXMLParser 值不被保留

转载 作者:行者123 更新时间:2023-11-29 05:06:49 27 4
gpt4 key购买 nike

这与我之前的问题类似。我没有得到答案,也许通过改变问题我可能会得到答案。

这是我的解析代码:

-(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>

最佳答案

看起来存在一些潜在的问题。在 didStartElement 方法中,您为每个元素分配/初始化一个新的 Items 对象并覆盖前一个元素。也许您可以将 Items init 移至您的 –parserDidStartDocument: 方法中。当你初始化时,它也应该看起来更像这样:

项目 *items = [[项目分配] init];self.theItems = 项目;[元素发布];

完成后您将获得正确的保留计数。

我还建议将 NSString @property 声明更改为复制而不是保留。代码:

theItems.imageURL = self.currentValue;
[self.currentValue setString:@""];

...没有按照您的想法行事。 theItems.imageURL 将指向您的 NSMutableString,然后您立即清除可变字符串,这意味着 imageURL 指向一个空的可变字符串。然后在所有其他迭代之后,它们都指向同一个空的 NSMutableString。如果您将 @property 声明更改为 copy,那么它将把 imageURL 设置为 self.currentValue 内容的不可变 NSString 副本。

关于iphone - NSXMLParser 值不被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com