2011-01-17 7 views
0

これは前の質問と似ています。私は答えを得ていなかった。質問を変えて答えが出るかもしれない。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.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 

ある

#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.h

である私の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> 
+1

コードを正しく書式設定し、可能であれば関連する部分に減らすことで、回答が多く得られる可能性が高くなります。これは読みにくいです。 – omz

+1

要素が開始されるたびに新しいtheItemsが作成され、古いものが置き換えられますが、古いものは保持されません。だから、基本的に、あなたは巨大なメモリリークを持っています。そして、あなたが終わったら、Itemは最後に解析された要素のデータを指しています。私はあなたが意図したものではないと思っています。 – spstanley

+0

私はブロックでコードを書式化しようとしましたが、私にとってはむしろ難しいようです。とにかく、たとえ私がviewDidLoadでtheItemsを作成しても、私は同じ結果を得ます。 – user574947

答えて

1

潜在的な問題がいくつかあるようです。 didStartElementメソッドでは、すべての要素に対して新しいItemsオブジェクトを割り当て/初期化し、前の要素を上書きします。おそらく、あなたは、項目initをあなたの-parserDidStartDocument:メソッドに移すことができます。あなたがinitを実行すると、次のようになります。

項目* items = [[Items alloc] init]; self.theItems = items; [items release];

これで、完了した時点で正しい保持回数が得られます。

また、NSStringの@property宣言をretainではなくcopyに変更することをお勧めします。コード:

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

...あなたの考えをしていません。 theItems.imageURLはあなたのNSMutableStringを指しているでしょう、そして、あなたはすぐに、imageURLが空の可変な文字列を指していることを意味する可変文字列を消去します。その後、他のすべての反復の後、それらのすべてが空の同じNSMutableStringを指しています。コピーする@property宣言を変更すると、imageURLはself.currentValueの内容の不変のNSStringコピーに設定されます。

+0

ありがとうございます。これは私を一歩近づけました。少なくとも私は間違っていることを知っています。ありがとうございました。 – user574947

関連する問題