iphone - NSXMLParser 值不被保留

标签 iphone ios nsxmlparser

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

这是我的解析代码:

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

相关文章:

iphone - Audio Toolbox 播放仅播放部分输出缓冲区

ios - 向当前包 ID 添加前缀会重置钥匙串(keychain)?

ios - 如何让 iOS 应用记住用户从 UIImagePickerController 选择的图像

ios - 滚动 Swift 时 tableView 的值重复

iphone - 在不同版本的操作系统上测试 iPhone 软件

iphone - 未调用 UIViewController 准备 segue 方法

ios - 前缀 - 配置有问题

xcode - iOS : NSXMLParser get attribute

xml - 如何将 url 传递给 NSBundle 以使用 AEXML 进行 xml 解析?

ios - 使用 nsxmlparser 解析 rss 提要时如何忽略非法字符?